Sub topic 4
The build context
This chapter assumes you can run docker build and docker run, and that you know what an image is. It covers what Docker sends to the builder when you build, and why that affects COPY and the build cache.
What the build context is
The build context is the set of files that Docker makes available to a build. You choose it with the last argument of docker build:
docker build -t myapp .The . is the context: the current directory and everything under it. The Dockerfile is normally read from the root of the context, but you can point to another one with -f.
The context is what COPY reads from. A COPY source path is resolved relative to the context root, not relative to your shell's current directory and not relative to the Dockerfile.
Why the context matters
Docker sends the whole context to the builder before it runs any instruction. This has three consequences.
- A large context makes every build slower, even if the Dockerfile copies only one file.
- Files outside the context cannot be copied. A path such as
COPY ../config.yml .fails because it points above the context root. - Any change to a file that a
COPYinstruction uses invalidates the cache for that instruction and every instruction after it.
A directory such as node_modules or .git can hold hundreds of megabytes. If it sits in your context, you pay for it on every build.
Choosing a context
The context does not have to be the current directory. Both of these are valid:
# Use a subdirectory as the context
docker build -t api ./services/api
# Use the repository root as the context, but a Dockerfile in a subdirectory
docker build -t api -f services/api/Dockerfile .The second form is useful when the image needs files from several directories, such as a shared library. The trade-off is a bigger context. Pair it with a .dockerignore file to keep the context small. A later chapter covers that file in detail.
A worked example
Create this layout:
demo/
Dockerfile
app.py
notes/
todo.txtUse this Dockerfile:
FROM alpine:3.20
WORKDIR /app
COPY app.py .
COPY notes/ ./notes/
CMD ["ls", "-R", "/app"]Build and run it from inside demo/:
docker build -t context-demo .
docker run --rm context-demoThe output lists app.py and notes/todo.txt under /app. Both were found because they are inside the context. If you build with docker build -t context-demo notes/, the context becomes notes/ and COPY app.py . fails, because app.py is no longer in the context. Docker also reports that it cannot find a Dockerfile there unless you pass -f.
Common errors
| Symptom | Likely cause |
|---|---|
| `COPY failed: file not found in build context` | The path is wrong relative to the context root, or `.dockerignore` excludes it. |
| `forbidden path outside the build context` | The `COPY` source uses `..` to leave the context. |
| Slow "Sending build context" step | Large directories such as `.git` or `node_modules` are in the context. |
| Cache misses on `COPY . .` | A file in the context changes on every build, such as a log file. |
Exercise
Your Dockerfile is in services/api/Dockerfile, and the image needs libs/shared/ from the repository root. Which docker build command works, and why does a plain docker build services/api fail?
Show answer
Run docker build -f services/api/Dockerfile . from the repository root. That makes the repository root the context, so COPY libs/shared/ ./shared/ can find the files. With docker build services/api, the context is services/api, so libs/shared/ lies outside it and cannot be copied.