简体   繁体   English

Dockerfile 无法复制 & Docker-compose 卷不与容器同步

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

相关问题 Docker 无法打开文件以在使用 django 的容器中运行 python 应用程序,使用 ZBAEDB53E845AE71F13945FCC00572 - Docker can't open file to run the python application in a container with django using docker-compose 无法从 docker-compose 容器打印到 Docker 桌面应用程序 - Can't print from docker-compose container to the Docker desktop app 无法连接到 mysql docker-compose - can't connect to mysql docker-compose Dockerfile中的pip和docker-compose怎么用? - How to use pip in Dockerfile with docker-compose? 如何使用 docker-compose 设置一个容器以允许它的整个卷访问另一个容器 - how to set one container to allow it's entire volume access to another container using docker-compose docker-compose音量未正确安装 - docker-compose volume not mounting correctly 为什么“ docker-compose run”可以在容器外部创建文件? - Why can “docker-compose run” create files outside the container? docker-compose exec导致[Errno 2]没有此类文件或目录:docker容器中的'docker-compose':'docker-compose' - docker-compose exec causes [Errno 2] No such file or directory: 'docker-compose': 'docker-compose' in docker container Docker中的烧瓶:Dockerfile构建和docker-compose中的不同相对导入 - Flask in docker: different relative import in Dockerfile build and docker-compose Docker-compose 无法导入 Django (ModuleNotFoundError) - Docker-compose can't import Django (ModuleNotFoundError)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM