Sub topic 2
Choosing the context with docker build
This chapter assumes you can write a simple Dockerfile with FROM, COPY and RUN, and that you have run docker build and docker run before. It covers how the last argument of docker build selects the build context, and how that choice interacts with the -f flag.
The PATH argument is the context
The build context is the set of files the builder can read during a build. You choose it with the final argument of docker build, called PATH. Docker sends the contents of that directory to the builder, and COPY and ADD can only reference files inside it.
# The final argument (.) is the build context: the current directory
docker build -t myapp .
# 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 .The first command uses the current directory as the context. The second uses only ./app, so files outside app/ are invisible to the build. The third changes which Dockerfile is read, but the context is still ..
The -f flag moves only the Dockerfile
Without -f, Docker looks for a file named Dockerfile at the root of the context. With -f, you name another file. The flag does not change the context. This is a common source of confusion: people write -f docker/Dockerfile.prod docker/ expecting the repository root to be available, and then COPY src/ ./src/ fails.
Pick the context first, then decide where the Dockerfile lives. If the Dockerfile needs files from several directories, the context must be a directory that contains all of them. That is often the repository root, even when the Dockerfile sits in a subdirectory.
How paths in COPY resolve
Source paths in COPY are relative to the context root, not to the Dockerfile and not to your shell's current directory.
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"]If you build this with docker build -t myapp ./app, then package.json means ./app/package.json. If you build it with docker build -t myapp . and package.json lives in app/, the build fails because the file is not at the context root.
Copying the dependency manifests before the rest of the source keeps the npm ci layer cached when only source files change.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `COPY failed: file not found in build context` | The file is outside the context, or the context is the wrong directory | Change the `PATH` argument, or move the file inside the context |
| `COPY ../shared .` fails | Paths cannot climb above the context root | Use a parent directory as the context and adjust the paths |
| `-f` used but `COPY` paths still fail | `-f` does not change the context | Set `PATH` to the directory the `COPY` paths are relative to |
| `unable to prepare context: path not found` | `PATH` does not exist | Check the directory name and your working directory |
A larger context has a cost
Choosing a bigger directory, such as the repository root, makes more files available to COPY. It also means more files are sent to the builder on every build. Later chapters show how .dockerignore limits this. For now, choose the smallest directory that contains everything the Dockerfile needs.
Exercise
You run docker build -t api -f services/api/Dockerfile services/api. The Dockerfile contains COPY ../../shared/ /shared/. The build fails. Why, and what change to the command and the Dockerfile fixes it?
Show answer
The context is services/api, and COPY cannot reference files outside the context, so ../../shared/ is not reachable. Use the repository root as the context: docker build -t api -f services/api/Dockerfile .. Then change the instruction to COPY shared/ /shared/, and any other COPY sources that were relative to services/api to start with services/api/.