Sub topic 7
COPY: bringing files into the image
This chapter assumes you can write a basic Dockerfile, run docker build, and know that the build context is the directory you pass to the build command. It covers how COPY works, how paths resolve, and the mistakes that break builds.
What COPY does
COPY takes files or directories from the build context and writes them into the image's filesystem. Each COPY creates a new layer.
COPY <src>... <dest>Sources are paths relative to the build context, not to the Dockerfile and not to your shell's working directory. The destination is a path inside the image. If it is relative, it resolves against the current WORKDIR.
Path rules
The rules below cause most COPY confusion.
- A source outside the context, such as
../secrets.txt, fails. The builder only receives the context. - A destination ending in
/is treated as a directory and created if missing. - When the source is a directory, its contents are copied, not the directory itself.
- Several sources require the destination to be a directory ending in
/. - Wildcards such as
*.txtare allowed in sources.
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
COPY src/ ./src/
COPY config/*.yaml /etc/myapp/Here requirements.txt lands in /app/requirements.txt. The contents of src/ land in /app/src/. The YAML files land in /etc/myapp/.
Copying in the right order
Copy the files that change rarely before the files that change often. This keeps the layer cache useful, as the next chapter explains in detail.
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]If only app.py changes, Docker reuses the cached layers up to the second COPY and skips the pip install. If requirements.txt changes, the install runs again.
Ownership and permissions
Files copied in are owned by root by default, even if a later USER instruction switches users. Use --chown to set ownership as part of the copy.
FROM node:20-slim
WORKDIR /app
COPY --chown=node:node package*.json ./
USER node
RUN npm ci
COPY --chown=node:node . .
CMD ["node", "server.js"]Setting ownership in COPY is cheaper than running RUN chown -R afterwards. A separate chown creates another layer that duplicates the changed files.
Copying from another stage
COPY --from=<stage> takes files from an earlier build stage instead of the context. This is the basis of multi-stage builds, which a later chapter covers.
COPY --from=build /out/app /appCommon errors
| Symptom | Likely cause |
|---|---|
| `file not found in build context` | The path is wrong, is outside the context, or is excluded by `.dockerignore`. |
| Directory structure is flattened | `COPY src /app` copies the contents of `src`. Use `COPY src/ /app/src/` to keep the folder. |
| Reinstalling dependencies on every build | `COPY . .` runs before the install step. |
| Permission denied at runtime | Files are owned by root and the process runs as a non-root user. Add `--chown`. |
Exercise
Exercise
A Dockerfile has WORKDIR /app followed by COPY . . and then RUN pip install -r requirements.txt. Each small code edit triggers a full dependency reinstall. What change fixes this, and why?
Show answer
Copy only requirements.txt first (COPY requirements.txt .), run pip install, and then run COPY . .. The install layer's cache key now depends only on requirements.txt. Editing source files invalidates only the later COPY . . layer, so the install layer is reused.
Exercise
You run COPY src /app and expect /app/src/main.py, but find /app/main.py. Why, and how do you get the expected layout?
Show answer
When the source is a directory, COPY copies its contents, not the directory itself. Use COPY src/ /app/src/ to recreate the src folder inside /app.