I have created a Django REST framework project that I want to deploy on a server. I do not have admin access to the server, and so, the only way I found to ensure that all the project dependencies (such as Anaconda distribution) will be available on the server, is to create a docker image for my project , then create a corresponding container on the server, then run it from there.
In my project, I have a python script (mymain.py) that gets called using subprocess.Popen(). This works fine locally, and subprocess.Popen() does everything it is supposed to do.
However, when I try to do it from the docker container, it seems as if the line subprocess.Popen() was completely skipped [mymain.py is not called].
For docker, I have created a docker-compose.yml file, and in command prompt I type:
docker-compose up
I do not see any error, and everything seem to be working fine, however subprocess.Popen() does not seem to be working.
mymain.py first line is:
print('Testing if subprocess called mymain.py!!!')
In a different file, I call subprocess.Popen(). I tried printing out the error, but unfortunately, both stdout and stderr return nothing:
p = subprocess.Popen(['python', mymain_path, args], shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, err = p.communicate()
print('SUBPROCESS ERROR: ' + str(err))
print('SUBPROCESS stdout: ' + str(out.decode()))
And this is what I get:
SUBPROCESS ERROR: None
SUBPROCESS stdout:
As you can see, the first line of my main.py was never printed...
However, when I do this locally by typing in command prompt:
python manage.py runserver 9000
everything works with no issues (the line 'Testing if subprocess called mymain.py!!!' gets printed).
I even tried to open the docker container shell and type the same 'python manage.py runserver 9000' command in there, but that did not work unfortunately.
Now the question is, how do I get subprocess to work remotely (in a docker container)?
Any help on this is very appreciated!
I am using:
Docker version 18.09.2, build 6247962
docker-compose version 1.23.2, build 1110ad01
Python 3.7.0
The subprocess module defines one class, Popen and a few wrapper functions that use that class. The constructor for Popen takes arguments to set up the new process so the parent can communicate with it via pipes. It provides all of the functionality of the other modules and functions it replaces, and more.
The main difference is that subprocess. run() executes a command and waits for it to finish, while with subprocess. Popen you can continue doing your stuff while the process finishes and then just repeatedly call Popen.
The always option always initiates a pull before creating the container.
Looks like a way to get rid of this issue is to: 1. use 'shell=False' 2. Not have a return value for the 'subprocess.Popen()'
subprocess.Popen(['python', mymain_path, args], shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With