简体   繁体   English

码头工人。 没有这样的文件或目录

[英]Docker. No such file or directory

I have some files which I want to move them to a docker container.我有一些文件,我想将它们移动到 docker 容器。 But at the end docker can't find a file..但最后docker找不到文件..

The folder with the files on local machine are at /home/katalonne/flask4包含本地机器上文件的文件夹位于/home/katalonne/flask4

File Structure if it matters:文件结构(如果重要):

如果重要的话,文件结构 The Dockerfile : Dockerfile

#
# First Flask App Dockerfile
#
#

# Pull base image.
FROM centos:7.0.1406

# Build commands
RUN yum install -y python-setuptools mysql-connector mysql-devel gcc python-devel
RUN easy_install pip
RUN mkdir /opt/flask4
WORKDIR /opt/flask4
ADD requirements.txt /opt/flask4
RUN pip install -r requirements.txt
ADD . /opt/flask4

# Define deafult command.
CMD ["python","hello.py"]

# Expose ports.
EXPOSE 5000

So I built the image with this command :所以我用这个命令构建了图像:

docker build -t flask4 .

I ran the container with volume by :我通过以下方式运行容器:

docker run -d -p 5000:5000 -v /home/Katalonne/flask4:/opt/flask4 --name web flask4

And when I want to run the file on the container :当我想在容器上运行文件时:

docker logs -f web

I get this error that it can not find my hello.py file :我收到这个错误,它找不到我的 hello.py 文件:

python: can't open file 'hello.py': [Errno 2] No such file or directory

What is my fault?我的错是什么?

PS : I'm a Docker and Linux partially-noob. PS:我是 Docker 和 Linux 部分菜鸟。

The files and directories that are located in the same location as your Dockerfile are indeed available (temporarily) to your docker build.与您的 Dockerfile 位于同一位置的文件和目录确实(暂时)可用于您的 docker 构建。 But, after the docker build, unless you have used ADD or COPY to move those files permanently to the docker container, they will not be available to your docker container after the build is done.但是,在 docker 构建之后,除非您使用ADDCOPY将这些文件永久移动到 docker 容器,否则它们在构建完成后将无法用于您的 docker 容器。 This file context is for the build, but you want to move them to the container.此文件上下文用于构建,但您希望将它们移动到容器中。

You can add the following command:您可以添加以下命令:

...
ADD . /opt/flask4
ADD . . 

# Define deafult command.
CMD ["python","hello.py"]

The line ADD . .ADD . . ADD . . should copy over all the things in your temporary build context to the container.应该将临时构建上下文中的所有内容复制到容器中。 The location that these files will go to is where your WORKDIR is pointing to (/opt/flask4).这些文件将转到的位置是您的WORKDIR指向的位置 (/opt/flask4)。

If you only wanted to add hello.py to your container, then use如果您只想将 hello.py 添加到您的容器中,请使用

ADD hello.py hello.py

So, when you run CMD ["python","hello.py"] , the pwd that you will be in is /opt/flask4 , and hello.py should be in there, and running the command python hello.py in that directory should work.因此,当您运行CMD ["python","hello.py"] ,您将使用的pwd/opt/flask4 ,并且 hello.py 应该在那里,并在其中运行命令python hello.py目录应该工作。

HTH.哈。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM