[英]Dockerfile can't copy & Docker-compose volume does not sync with container
I am using Docker windows desktop client and trying to build a Flask python container.我正在使用 Docker windows 桌面客户端并尝试构建 Flask Z23EEEB4347BDD2575DDFC6B7EE9A3 容器。 The dockerfile copies
requirements.txt
and flask-script.py
files into python container and sets it up. dockerfile 将
requirements.txt
和flask-script.py
文件复制到 python 容器中并进行设置。
|-root directory (this is where the powershell commands are run from)
|-docker-compose.yml
|-src
| └── Dockerfile
| ├── flask-script.py
| ├── requirements.txt
Docker-compose.yml: Docker-compose.yml:
version: '3'
services:
flask-service:
build: ./src
volumes:
- ./src:/usr/src/app
ports:
- 8080:80
src/Dockerfile: src/Dockerfile:
FROM python:3
COPY . /usr/src/app
RUN pip install --no-cache-dir -r requirements.txt
CMD [ "python", "./flask-script.py" ]
Running docker-compose up
gives the following error:运行
docker-compose up
会出现以下错误:
ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'
BUT If I change the dockerfile to:但是,如果我将 dockerfile 更改为:
FROM python:3
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD [ "python", "./flask-script.py" ]
It does work, however after the docker-compose up
the volume specified in docker-compose does not sync with the container.它确实有效,但是在
docker-compose up
docker-compose 中指定的卷后,它不会与容器同步。 If I make any changes to the flask-script.py
file they are not seen by the container.如果我对
flask-script.py
文件进行任何更改,则容器不会看到它们。
What am I doing wrong?我究竟做错了什么?
It does work when you add the WORKDIR
because you have all files in that directory.添加
WORKDIR
时它确实有效,因为您在该目录中拥有所有文件。
However, in your first Dockerfile, you copy everything to /usr/src/app
, but you don't change your workdir to that directory, therefore the requirements.txt
is not reachable as it's located in /root/requirements.txt
但是,在您的第一个 Dockerfile 中,您将所有内容复制到
/usr/src/app
,但您没有将工作目录更改为该目录,因此requirements.txt
无法访问,因为它位于/root/requirements.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.