问题
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
回答1:
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)
回答2:
Had exactly the same problem, none of previous fixes worked for me.
Finally I tried
print("Hello world!", flush=True)
which seems to work in my environment (docker, python3.7)
来源:https://stackoverflow.com/questions/54714478/python-subprocess-popen-not-working-in-a-docker-container-works-fine-locally