简体   繁体   English

构建 Docker 映像时“没有此类文件或目录”

[英]“No such file or directory” when building Docker image

I have an existing and working Dockerfile, that I want to update to have a better structure, and to allow me to volume-mount part of the application directory to enable persistence.我有一个现有的和正在工作的 Dockerfile,我想对其进行更新以获得更好的结构,并允许我对应用程序目录的一部分进行卷挂载以启用持久性。

Folder structure is:文件夹结构为:

.git/
.gitignore
Dockerfile
README.md
Sorter.py
WebService.py
requirements.txt

The current working Dockerfile is:当前工作的 Dockerfile 为:

FROM python:3.8-alpine
ADD . /
RUN pip install -r requirements.txt
EXPOSE 5000
CMD [ "python", "./WebService.py" ]

I now want to simply do this not in the root directory, so I do the following:我现在想简单地不在根目录中执行此操作,因此我执行以下操作:

FROM python:3.8-alpine
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["WebService.py"]

I build the image "docker build -t, and run it using the same docker-compose file as for the first buildscript - and I get the error:我构建图像“docker build -t,并使用与第一个 buildscript 相同的 docker-compose 文件运行它 - 我收到错误:

"python: can't open file '/app/WebService.py': [Errno 2] No such file or directory"

The problem is with this line WORKDIR /app , as you can see the Docker tries to run the file /app/WebService.py , but you do not have it.问题在于这一行WORKDIR /app ,您可以看到 Docker 尝试运行文件/app/WebService.py ,但您没有它。 Your file is in .您的文件位于. not in app folder.不在app文件夹中。 So either you should create the app folder and put your WebService.py in that folder, either just remove WORKDIR /app .因此,要么您应该创建app文件夹并将您的WebService.py放在该文件夹中,要么只需删除WORKDIR /app

Here's the solution I found:这是我找到的解决方案:

FROM python:3.8-alpine
RUN mkdir /app
ADD . /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 5000
CMD [ "python", "./WebService.py" ]

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

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