Sub topic 3
The build context
This chapter assumes you can run docker build and docker run, and that you know an image is built from a Dockerfile. It explains what Docker sends to the builder when you build, and why that matters for COPY.
What the build context is
The build context is the set of files that docker build makes available to the builder. You choose it with the last argument of the command.
docker build -t myapp:1.0 .The . is the context: the current directory and everything under it. The Dockerfile is normally read from the context root, but the context and the Dockerfile are separate ideas. The Dockerfile is the recipe. The context is the pantry the recipe can take from.
Why the context limits COPY
COPY and ADD can only read files that are inside the context. The builder cannot see the rest of your disk. Source paths are relative to the context root, not to the Dockerfile and not to your shell's working directory.
FROM node:20-slim
WORKDIR /app
COPY package*.json ./
COPY src/ ./src/With context ., COPY src/ ./src/ reads ./src from the directory you passed to docker build. A path such as ../shared fails, because it leaves the context.
Choosing the context and the Dockerfile separately
Use -f to point at a Dockerfile that is not at the context root. The context stays the last argument.
docker build -f docker/Dockerfile.prod -t myapp:prod .Here the Dockerfile lives in docker/, but COPY paths are still relative to .. This is the usual fix when a repository keeps its Dockerfiles in a subfolder but needs files from the root.
The cost of a large context
Docker transfers the context to the builder before it runs the first instruction. A large context slows every build, even when the cache would skip all the steps. A context that contains node_modules, .git history, build output, or large data files can add many megabytes or gigabytes to each transfer.
BuildKit, the default builder in current Docker versions, sends only the files the Dockerfile needs. This reduces the cost, but it does not remove the risk. It is still best to keep the context small and to avoid pointing it at a directory such as your home folder.
The larger risk is what ends up in the image. COPY . . copies everything in the context, including .env files, private keys, and local dependencies. Anything you copy is stored in a layer, and deleting it in a later RUN does not remove it from the earlier layer.
A .dockerignore file in the context root excludes paths from the context. The next chapters cover it in detail, but a minimal example shows the idea:
node_modules
.git
*.log
.envCommon errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `COPY failed: file not found in build context` | The path is wrong, or the file is excluded by `.dockerignore` | Check the path relative to the context root and review `.dockerignore` |
| `forbidden path outside the build context` | The source uses `../` | Move the context up a level and use `-f` for the Dockerfile |
| Build is slow before step 1 | The context is large | Add a `.dockerignore` and build from a narrower directory |
| Secrets appear in the image | `COPY . .` copied them | Exclude them and avoid copying secrets into layers |
Check what the builder receives
A quick way to see the effect of the context is to build a tiny image that lists its files.
FROM alpine:3.20
WORKDIR /ctx
COPY . .
CMD ["ls", "-a"]docker build -t ctx-check .
docker run --rm ctx-checkThe output lists exactly what COPY . . brought in. Add a .dockerignore entry and rebuild to see a file disappear.
Exercise
Your Dockerfile is at docker/Dockerfile and needs package.json from the repository root. Which command builds it, and what does COPY package.json ./ read from?
Show answer
Run docker build -f docker/Dockerfile -t myapp . from the repository root. The context is ., so COPY package.json ./ reads package.json from the repository root, not from the docker/ folder.
Exercise
Why is it not enough to delete .env with RUN rm .env after COPY . .?
Show answer
Each instruction creates a layer, and the earlier layer that contains .env stays in the image. Anyone who pulls the image can read it. Exclude the file with .dockerignore so it never enters the context.