Docker container fails to run, Error : python3: can't open file 'flask run --host=0.0.0.0': [Errno 2] No such file or directory
我是Docker的新手,我正在尝试将python flask Microservice进行dockerize。 docker文件构建成功,但是在运行容器时出现错误:
1 | python3: can't open file 'flask': [Errno 2] No such file or directory |
我假设我的docker文件在COPY路径,ENTRYPOINT或CMD中存在一些错误,即我用来运行flask应用程序的命令。我无法找出错误。
Ubuntu机器上的目录结构为:
1 | /home/ubuntu/Docker/auth |
目录auth包含我的Dockerfile和所有其他python flask文件:
1 2 | $ls Dockerfile run.py views.py resources.py models.py |
run.py是要执行的主要python flask文件。我确定在我对烧瓶应用程序执行CMD命令的方式中存在语法错误,并且它找不到执行的run.py。我无法纠正该错误。
映像成功构建。为了运行容器,我使用:
1 | docker build <imageid> |
Docker文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | FROM ubuntu:16.04 MAINTAINER xyz <xyz@yahoo.com> RUN apt-get update \ && apt-get install -y software-properties-common vim \ && add-apt-repository ppa:jonathonf/python-3.6 \ && apt-get update -y \ && apt-get install -y build-essential python3.6 python3.6-dev python3-pip python3.6-venv \ && pip3 install --upgrade pip WORKDIR /auth COPY . /auth RUN pip3 install alembic==0.9.9 \ && pip3 install Flask==1.0.2 \ ENTRYPOINT ["python3" ] CMD ["export","FLASK_APP=run.py" ] CMD ["set","FLASK_APP=run.py" ] CMD ["flask","run","--host=0.0.0.0" ] |
预期:应用程序应在容器上运行。
实际:Python3:无法打开文件"烧瓶":[Errno 2]没有此类文件或目录
The best use for ENTRYPOINT is to set the image’s main command,
allowing that image to be run as though it was that command (and then
use CMD as the default flags).
https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#entrypoint
很多人似乎错过了关于
我不确定您是否应该再有一条
1 | RUN mkdir some/dir |
现在:
run.py is the main python flask file for execution
因此,我建议您将其定义为入口点:
1 | ENTRYPOINT ["./run.py" ] |
您可能还希望每次容器启动时都运行的命令,例如
-
将命令移到
run.py 文件中要么
-
保持
CMD ["flask","run","--host=0.0.0.0" ] 行。该命令将作为参数传递给run.py入口点,因此您可以在其中执行它。这样,当您使用替代参数运行容器时,您可以轻松地覆盖命令。
这些东西也在文档中:
Understand how CMD and ENTRYPOINT interact
Both CMD and ENTRYPOINT
instructions define what command gets executed when running a
container. There are few rules that describe their co-operation.Dockerfile should specify at least one of CMD or ENTRYPOINT commands.
ENTRYPOINT should be defined when using the container as an
executable.CMD should be used as a way of defining default arguments for an
ENTRYPOINT command or for executing an ad-hoc command in a container.CMD will be overridden when running the container with alternative
arguments.