简体   繁体   English

Docker容器无法运行,错误:python3:无法打开文件“ flask run --host = 0.0.0.0”:[Errno 2]没有此类文件或目录

[英]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

I am new to docker and I am trying to dockerize a python flask microservice. 我是Docker的新手,我正在尝试将python flask Microservice进行dockerize。 The docker file builds successfully but on running the container it gives an error : docker文件构建成功,但是在运行容器时出现错误:

python3: can't open file 'flask': [Errno 2] No such file or directory

I am assuming there is some mistake in my docker file either in the COPY path, ENTRYPOINT or CMD ie the commands that I use to run the flask application. 我假设我的docker文件在COPY路径,ENTRYPOINT或CMD中存在一些错误,即我用来运行flask应用程序的命令。 I am not able to figure out the mistake. 我无法找出错误。

The directory structure on Ubuntu machine is: Ubuntu机器上的目录结构为:

/home/ubuntu/Docker/auth

The directory auth contains my Dockerfile and all other python flask files: 目录auth包含我的Dockerfile和所有其他python flask文件:

$ls 
Dockerfile   run.py    views.py     resources.py    models.py

run.py is the main python flask file for execution. run.py是要执行的主要python flask文件。 I'm sure there is some syntax error in how I am executing the CMD command for the flask application and it is not able to find run.py for execution. 我确定在我对烧瓶应用程序执行CMD命令的方式中存在语法错误,并且它找不到执行的run.py。 I am not able to correct that error. 我无法纠正该错误。

The image builds successfully. 映像成功构建。 For running the container I use: 为了运行容器,我使用:

docker build <imageid>

Dockerfile Docker文件

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

Expected: Application should run on the container. 预期:应用程序应在容器上运行。 Actual: Python3: can't open file 'flask': [Errno 2] No such file or directory 实际: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). ENTRYPOINT的最佳用途是设置映像的主命令,以使该映像像该命令一样运行(然后使用CMD作为默认标志)。

https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#entrypoint https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#entrypoint

lots of people seem to miss this point about the ENTRYPOINT and CMD Dockerfile-instructions. 许多人似乎错过了关于ENTRYPOINTCMD Dockerfile指令的这一点。

the ENTRYPOINT instruction supposed to run some executable, which should run every time you start the container, such as starting your server. ENTRYPOINT指令应运行一些可执行文件,该文件应在每次启动容器时运行,例如启动服务器。

the CMD supposed to include the flags provided to that executable, so they can be easily overridden when running the container. CMD应该包含提供给该可执行文件的标志,因此在运行容器时可以轻松地覆盖它们。

i am not sure you are supposed to have more then one CMD instruction. 我不确定您是否应该拥有一份以上的CMD指令。 if you need to run commands during the build process, you can use the RUN instruction - for example: 如果需要在构建过程中运行命令,则可以使用RUN指令-例如:

RUN mkdir some/dir

now: 现在:

run.py is the main python flask file for execution run.py是要执行的主要python flask文件

hence i suggest you define it as your entrypoint: 因此,我建议您将其定义为入口点:

ENTRYPOINT [ "./run.py" ]

commands that you may also want to run every time the container starts, such as flask run --host=0.0.0.0 you can: 您可能还希望每次容器启动时都运行的命令,例如flask run --host=0.0.0.0您可以:

  • move that command to sit inside the run.py file 将命令移到run.py文件中

    or 要么

  • keep the CMD [ "flask", "run", "--host=0.0.0.0" ] line. 保持CMD [ "flask", "run", "--host=0.0.0.0" ]行。 this command will be passed as an argument to the run.py entrypoint, so you may execute it in there. 该命令将作为参数传递给run.py入口点,因此您可以在其中执行它。 that way you can easily override the command when running the container with alternative arguments. 这样,当您使用替代参数运行容器时,您可以轻松地覆盖命令。

this stuff is also in the docs : 这些东西也在docs中

Understand how CMD and ENTRYPOINT interact 了解CMD和ENTRYPOINT如何相互作用

Both CMD and ENTRYPOINT instructions define what command gets executed when running a container. CMD和ENTRYPOINT指令均定义运行容器时执行的命令。 There are few rules that describe their co-operation. 很少有规则描述他们的合作。

Dockerfile should specify at least one of CMD or ENTRYPOINT commands. Dockerfile应至少指定CMD或ENTRYPOINT命令之一。

ENTRYPOINT should be defined when using the container as an executable. 使用容器作为可执行文件时,应定义ENTRYPOINT。

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用作为ENTRYPOINT命令定义默认参数或在容器中执行自定义命令的方式。

CMD will be overridden when running the container with alternative arguments. 使用替代参数运行容器时,CMD将被覆盖。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在docker容器上运行flask应用程序:错误:python:无法打开文件&#39;//run.py&#39;:[Errno 2]没有这样的文件或目录Docker错误 - having on running flask app on docker container : Error: python: can't open file '//run.py': [Errno 2] No such file or directory Docker Error python:无法打开文件“manage.py”:[Errno 2] 没有这样的文件或目录 docker-compose run - python: can't open file 'manage.py': [Errno 2] No such file or directory docker-compose run 当运行 docker-compose up 我得到 python: can&#39;t open file &#39;manage.py&#39;: [Errno 2] No such file or directory - When run docker-compose up I get python: can't open file 'manage.py': [Errno 2] No such file or directory 内部 Docker 容器 - python:无法打开文件 './services/web/manage.py':[Errno 2] 没有这样的文件或目录 - Inside Docker Container - python: can't open file './services/web/manage.py': [Errno 2] No such file or directory Docker for windows10 运行 django 失败:无法打开文件“manage.py”:[Errno 2] 没有那个文件或目录 - Docker for windows10 run django fail: Can't open file 'manage.py': [Errno 2] No such file or directory 节点JS生成结果:python3:无法打开文件'./test':[Errno 2]没有这样的文件或目录 - Node JS spawn result: python3: can't open file './test': [Errno 2] No such file or directory python3: 无法打开文件 &#39;model_main_tf2.py&#39;: [Errno 2] 没有那个文件或目录 - python3: can't open file 'model_main_tf2.py': [Errno 2] No such file or directory Python3:无法打开文件'sherlock.py' [Errno 2] 没有这样的文件或目录 - Python3: can't open file 'sherlock.py' [Errno 2] No such file or directory Docker 无法打开文件以在使用 django 的容器中运行 python 应用程序,使用 ZBAEDB53E845AE71F13945FCC00572 - Docker can't open file to run the python application in a container with django using docker-compose 在docker上运行时,Python烧瓶重新加载器“Errno 2没有这样的文件或目录”错误 - Python flask reloader “Errno 2 No such file or directory” error when running on docker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM