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

2024-09-30 08:19:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我的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"]

当我跑步时:

sudo docker build -t test-app .

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)


Tags: installpipthedjangorunpydev脚本
2条回答

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

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

RUN ["/bin/python", "appmanage.py", "appAdd.json"]

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

替换

RUN appmanage.py appAdd.json

RUN PYTHON_SITE_PACKAGES="$(python -c 'import site; print(site.getsitepackages()[0])')" \
    && python $PYTHON_SITE_PACKAGES/<package>/appmanage.py

相关问题 更多 >

    热门问题