如何在docker中运行基于pip安装的python脚本?

如何在docker中运行基于pip安装的python脚本?,python,django,docker,dockerfile,Python,Django,Docker,Dockerfile,我的docker文件设计如下: #Use python 3.6 image FROM python:3.6 ENV PYTHONUNBUFFERED 1 #install required packages RUN apt-get update RUN apt-get install libsasl2-dev libldap2-dev libssl-dev python3-dev psmisc -y #install a pip package #Note: This pip package

我的docker文件设计如下:

#Use python 3.6 image
FROM python:3.6
ENV PYTHONUNBUFFERED 1

#install required packages
RUN apt-get update
RUN apt-get install libsasl2-dev libldap2-dev libssl-dev python3-dev psmisc -y

#install a pip package
#Note: This pip package has a completely configured django project in it
RUN pip install <pip-packge>

#add a configuration file required for setup
ADD appAdd.json /

#Run a script
#Note: Here appmanage.py is a file inside the pip installed location, but it will be accesible directly without cd to the folder
RUN appmanage.py appAdd.json

#The <pip-packge> installed comes with a built in django package, so running it with following CMD
#Note: Here manage.py is present inside the pip package folder but it is accesible directly
CMD ["manage.py","runserver","0.0.0.0:8000"]
RUN ["/bin/python", "appmanage.py", "appAdd.json"]
python脚本运行部分将在功能方面获得成功,但不会创建映像,因为此时它会退出,并出现以下错误:

The command '/bin/sh -c appmanage.py appAdd.json' returned a non-zero code: 137
是否将其视为shell脚本而不是python脚本。我如何克服这个问题并成功地运行django项目

注意:在本地环境中,我可以在我的机器中执行这些步骤并成功安装。因此,pip包附带的django项目的代码没有问题

更新
脚本appmanage.py在端口9999中运行django项目,并执行一些测试并终止端口9999。脚本中的kill操作是否导致上述错误(137)?

您需要使用appmanage.py的完整路径。要找到它,您可以使用SitePackages目录

替换

RUN appmanage.py appAdd.json

runpython\u SITE\u PACKAGES=“$(PYTHON-c'导入站点;打印(SITE.getsitepackages()[0])”\
&&python$python\u站点\u包//appmanage.py

我们可以使用run将sh shell更改为python shell

只需按如下方式修改运行部件:

#Use python 3.6 image
FROM python:3.6
ENV PYTHONUNBUFFERED 1

#install required packages
RUN apt-get update
RUN apt-get install libsasl2-dev libldap2-dev libssl-dev python3-dev psmisc -y

#install a pip package
#Note: This pip package has a completely configured django project in it
RUN pip install <pip-packge>

#add a configuration file required for setup
ADD appAdd.json /

#Run a script
#Note: Here appmanage.py is a file inside the pip installed location, but it will be accesible directly without cd to the folder
RUN appmanage.py appAdd.json

#The <pip-packge> installed comes with a built in django package, so running it with following CMD
#Note: Here manage.py is present inside the pip package folder but it is accesible directly
CMD ["manage.py","runserver","0.0.0.0:8000"]
RUN ["/bin/python", "appmanage.py", "appAdd.json"]

是的,我试过了,这是错误:
python3:无法打开文件'appmanage.py':[Errno 2]没有这样的文件或目录
,因为该文件位于pip包路径(站点包)内。在这种情况下,请尝试使用
appmanage.py
的完整路径。如何获取pip路径(安装包的位置)?是否存在任何环境变量?请参阅我的更新答案。您正在安装的pip包是什么?稍微修改了命令以保留变量的值(编辑了答案),但问题没有解决,脚本正在执行,但在脚本完成后,生成将以问题中提到的相同错误(137)退出。这没有帮助,请阅读雅库布的答案和评论是的,我更新了我的答案。这也可能是你的答案,但我的答案是另一种方式。如果您没有
/bin/python
请尝试“/bin/python3”。此外,我还取出了
-c
选项,因为它不是必需的。