Sub topic 4
How COPY and ADD resolve paths
This chapter assumes you can write a basic Dockerfile and run docker build. It also assumes you know that the final argument of docker build selects the build context. Here you will see how COPY and ADD turn the paths you write into files inside the image.
Sources are relative to the context root
The builder doesn't see your whole disk. It sees only the build context. Every source path in COPY or ADD is resolved against the root of that context, not against the Dockerfile's location and not against WORKDIR.
FROM node:20-slim
WORKDIR /app
# Left side: relative to the context root
# Right side: relative to WORKDIR (/app)
COPY package.json package-lock.json ./
RUN npm ci
COPY src/ ./src/
CMD ["node", "src/server.js"]Suppose you run docker build -t myapp ./app. The context root is then ./app, so COPY package.json ./ reads ./app/package.json. If you run docker build -t myapp -f docker/Dockerfile.prod ., the context root is still the current directory. The -f flag changes only which Dockerfile is read.
Destinations are relative to WORKDIR
The destination is a path inside the image. A relative destination such as ./ or config/ is resolved against the current WORKDIR. An absolute destination such as /etc/app/ ignores WORKDIR. If the destination directory doesn't exist, Docker creates it.
If you copy more than one source, or use a wildcard that may match several files, the destination must end in /. Otherwise the build fails.
What you cannot reference
Sources must be inside the context. These cases surprise people:
- Parent paths fail.
COPY ../shared/config.json .is rejected, because the file is outside the context. Either widen the context (for example, build from the repository root) or restructure the layout. - Absolute sources are still relative to the context.
COPY /package.json ./readspackage.jsonfrom the context root, not from the filesystem root of your machine. - Ignored files do not exist. A file excluded by
.dockerignoreis not sent to the builder.COPYthen fails with a "not found" error, even though the file is on your disk. - Symlinks pointing outside the context are not followed to their targets.
Files versus directories
Copying a directory copies its contents, not the directory itself. COPY src/ ./src/ creates /app/src with the files inside. COPY src/ ./ puts those files directly in /app. This is a common source of "my files are in the wrong place" bugs.
Wildcards follow Go's path matching rules:
FROM busybox
WORKDIR /data
COPY config/*.json ./config/
COPY file?.txt ./ADD and how it differs
ADD resolves local paths exactly like COPY. It adds two behaviors:
- If the source is a local tar archive (such as
.tar.gz),ADDunpacks it into the destination. - If the source is a URL,
ADDdownloads it. A downloaded archive is not unpacked.
Because of these extras, ADD can do things you did not intend. Use COPY unless you need one of those two behaviors.
Common errors
| Error message | Likely cause |
|---|---|
| `forbidden path outside the build context` | The source uses `../` or resolves outside the context. |
| `not found` | The path is wrong relative to the context root, or `.dockerignore` excluded it. |
| `When using COPY with more than one source file, the destination must be a directory and end with a /` | Multiple sources with a destination that lacks a trailing slash. |
Exercise
Your project has docker/Dockerfile and app/main.py. You run docker build -t demo -f docker/Dockerfile . from the project root. Which path must the Dockerfile use to copy main.py into /srv/, and why does COPY ../app/main.py /srv/ fail if you instead run the build from inside docker/ with . as the context?
Show answer
Use COPY app/main.py /srv/, because the context root is the project root. If the context is docker/, then app/ lies outside it, so ../app/main.py is rejected as a path outside the build context.