Search code examples
pythondockerdocker-composedockerfiledocker-volume

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. The dockerfile copies requirements.txt and flask-script.py files into python container and sets it up.

|-root directory (this is where the powershell commands are run from)
|-docker-compose.yml
|-src
|  └── Dockerfile
|  ├── flask-script.py
|  ├── requirements.txt

Docker-compose.yml:

version: '3'

services:
  flask-service:
    build: ./src
    volumes:
      - ./src:/usr/src/app
    ports:
      - 8080:80

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:

ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'

BUT If I change the dockerfile to:

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. If I make any changes to the flask-script.py file they are not seen by the container.

What am I doing wrong?


Solution

  • It does work when you add the WORKDIR because you have all files in that directory.

    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