0

I have the following file structure:

enter image description here

Dockerfile:

# syntax=docker/dockerfile:1

FROM python:3.8-slim-buster

WORKDIR /home/pos

COPY requirements.txt requirements.txt

RUN pip3 install -r requirements.txt

COPY /src /src
    
CMD ["python", "src/manage.py", "runserver"]

I expect that the content of the src/ folder will be copied to the same path in the container (home/pos/src/) but only the requirements.txt are copied to that path, the content of the /src folder is being copied to the "root" (/), so I need to change the command to:

COPY /src /home/pos/src

It is necessary to set the WORKDIR for each command?

1 Answers1

2

If you use an absolute path on the right-hand side of COPY then it uses an absolute path in the container. If you use a relative path then it is relative to the current WORKDIR. In your Dockerfile you have an absolute path /src so that's the destination, but if you change it to a relative path

COPY ./src/ ./src/

it will be under the current WORKDIR.

I'd suggest avoiding as many paths and filenames as possible on the right-hand side of COPY; for example you can also COPY requirements.txt ./.

(The rules for the left-hand side of COPY are a little different: all leading .. path components are stripped, then regardless of whether it's an absolute or relative path it's interpreted as a path underneath the build-context directory. So even if the source looks like an absolute path it's often interpreted as a path relative to the Dockerfile. Again, using a relative path on the left-hand side of COPY is probably clearer.)

David Maze
  • 130,717
  • 29
  • 175
  • 215