Python subprocess.Popen()在Docker容器中不起作用-在本地工作正常 [英] Python subprocess.Popen() not working in a docker container - works fine locally

查看:91
本文介绍了Python subprocess.Popen()在Docker容器中不起作用-在本地工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个我想部署在服务器上的Django REST框架项目.我没有对该服务器的管理员访问权限,因此,我发现确保所有项目依赖项(例如Anaconda发行版)在服务器上均可用的唯一方法是为我的项目创建一个docker映像,然后创建服务器上的相应容器,然后从那里运行它.

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.

在我的项目中,我有一个使用subprocess.Popen()调用的python脚本(mymain.py).这在本地可以正常工作,subprocess.Popen()可以完成它应该做的所有事情.

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.

但是,当我尝试从docker容器执行此操作时,似乎完全跳过了subprocess.Popen()行[未调用mymain.py].

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].

对于docker,我已经创建了docker-compose.yml文件,并在命令提示符下键入:

For docker, I have created a docker-compose.yml file, and in command prompt I type:

docker-compose up

我没有看到任何错误,并且一切似乎都正常工作,但是subprocess.Popen()似乎没有工作.

I do not see any error, and everything seem to be working fine, however subprocess.Popen() does not seem to be working.

mymain.py第一行是:

mymain.py first line is:

print('Testing if subprocess called mymain.py!!!')  

在另一个文件中,我称为subprocess.Popen().我尝试打印出错误,但不幸的是,stdout和stderr均未返回任何内容:

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()))    

这就是我得到的:

SUBPROCESS ERROR: None
SUBPROCESS stdout:

如您所见,我的main.py的第一行从未打印过...

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

一切正常,没有任何问题(打印测试子进程名为mymain.py !!!的行").

everything works with no issues (the line 'Testing if subprocess called mymain.py!!!' gets printed).

我什至试图打开docker容器外壳,并在其中键入相同的"python manage.py runserver 9000"命令,但不幸的是,该命令无法正常工作.

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.

现在的问题是,如何使子进程远程工作(在Docker容器中)?

Now the question is, how do I get subprocess to work remotely (in a docker container)?

对此表示任何帮助!

我正在使用:

Docker version 18.09.2, build 6247962
docker-compose version 1.23.2, build 1110ad01
Python 3.7.0

推荐答案

一种解决此问题的方法是:1.使用'shell = False'2.'subprocess.Popen()'没有返回值

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) 

这篇关于Python subprocess.Popen()在Docker容器中不起作用-在本地工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆