简单的Pythondocker脚本

我有一个非常简单的Python脚本。 我试图让它从docker运行。 我简化了它,现在只是打印hello world

我的docker设置:

[搬运工-compose.yaml]

 version: '2' services: dev: build: . volumes: - ./app.py:/app.py 

[Dockerfile]

 FROM ubuntu RUN apt-get update -y RUN apt-get install -y python python-pip python-dev build-essential libpq-dev ADD ./requirements /code/requirements RUN pip install --upgrade pip RUN pip install -r /code/requirements/base.txt VOLUME /app.py CMD ["python","/app.py"] 

当我运行docker-compose时,出现以下错误:

错误:为开发无法启动服务开发:oci运行时错误:执行:“Python”:可执行文件找不到$ PATH

但是,docker应该被构build到Ubuntu Image中。 这是我用我的Python Flask脚本相同的图像,但没有任何问题。

既然你只是想打印一个'Hello world'(或者其他不需要额外库的东西),你可以使用下面的模板:

[搬运工-compose.yml]

 version: '2' services: my_service: build: ./<service_dir_name> # replace <.> with the directory containing your files in the current directory where .yml is located. 

[Dockerfile]

 FROM python:2.7-alpine # Copy your files in this order: <files in the current dir> <destination in the container> COPY codes/ /app WORKDIR app ENTRYPOINT python -u my_code.py 

高山是一个非常轻的linux与python预装。 如果你需要添加一些库,添加

 RUN pip install <library> 

到Dockerfile。 你的python代码位于

 <files in the current dir> 

希望有所帮助。

看来你安装的python不能被Ubuntu发现。 我build议添加到您的dockerfile(安装python的行后面):

 RUN PYTHONPATH=/usr/lib/python2.7 RUN export $PYTHONPATH RUN source ~/.bashrc 

要么

 RUN PYTHONPATH=/usr/lib/python3.4 RUN export $PYTHONPATH RUN source ~/.bashrc 

如果你宁愿使用Python3

这会将你的Pythonpath添加到你的bash寄存器文件,并使其生效。