Sub topic 1
What the Build Context Is
Prerequisites
This chapter assumes you can do the following:
- Use the command line.
- Write a simple Dockerfile with
FROM,COPYandRUN. - Run
docker buildanddocker run.
The context is a set of files
When you run docker build, the Dockerfile is only part of the input. The build also needs the files your image will contain, such as source code, dependency manifests and configuration. The set of files made available to the build is called the build context.
The context is a directory tree, chosen by the last argument of docker build. Everything in that tree is a candidate for the image. Nothing outside it can be used by the build.
# The final argument (.) is the build context: the current directory
docker build -t myapp .Here . means the context is the current directory and everything under it.
Why the context exists
The Docker CLI and the builder are separate programs. The builder (BuildKit, running inside the Docker daemon or in a remote service) may be on a different machine from the one where you typed the command. It cannot read your disk directly. The CLI therefore sends the context to the builder before the build can use it.
The diagram below shows the flow.
With BuildKit, the builder requests only the files it actually needs, rather than always receiving the whole tree. Even so, it is safest to think of the whole context as visible to the build.
What COPY can reach
Paths on the left side of COPY and ADD are resolved against the context root, not against your shell's current directory and not against the Dockerfile's location.
FROM node:20-slim
WORKDIR /app
# Paths on the left are relative to the build context root
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
CMD ["node", "server.js"]In this Dockerfile, package.json means <context>/package.json. COPY . . copies the whole context into /app.
Two consequences follow:
- A file outside the context cannot be copied.
COPY ../config.yml .fails, because..would leave the context. - A file inside the context is available to
COPY, whether or not you meant to include it.
The context is not the Dockerfile's directory
A common mistake is to assume the context is wherever the Dockerfile sits. By default the Dockerfile is looked for at the context root, so the two often coincide. They are separate settings, though. The -f flag changes which Dockerfile is used and leaves the context alone.
# Use a subdirectory as the context
docker build -t myapp ./app
# Dockerfile in a different place; context is still the current directory
docker build -t myapp -f docker/Dockerfile.prod .In the second command the context is ., even though the Dockerfile is in docker/. A COPY src/ ./src/ in that Dockerfile still reads ./src/ from the current directory.
Common errors
| Symptom | Likely cause |
|---|---|
| `COPY failed: file not found in build context` | The path is wrong relative to the context root, or the file is outside the context. |
| `COPY ../x .` is rejected | The source path leaves the context. Choose a context higher up the tree. |
| Build is slow before the first instruction runs | The context is large, so sending it takes time. |
| A file you expected is missing | A `.dockerignore` rule excluded it. |
Exercise
You run docker build -t api -f services/api/Dockerfile . from the repository root. Inside that Dockerfile, which directory does COPY shared/ /shared/ read from, and why?
Show answer
It reads shared/ at the repository root. The context is the final argument (.), which is the repository root. The -f flag only selects which Dockerfile to use, so COPY sources are resolved against the context and not against services/api/.