Sub topic 3
Where the Dockerfile lives and the -f flag
This chapter assumes you can run docker build and know that its last argument is the build context. Here you will learn how Docker finds the Dockerfile and how to point it at a different one.
The default: a file named Dockerfile at the context root
If you give no -f flag, Docker looks for a file named Dockerfile at the root of the build context. The context is the PATH argument you pass to docker build.
# Looks for ./Dockerfile
docker build -t myapp .
# Looks for ./app/Dockerfile
docker build -t myapp ./appThe Dockerfile location and the context are two separate settings. By default the first is derived from the second, but you can set them independently.
Choosing another file with -f
The -f (or --file) flag names the Dockerfile to use. It does not change the context.
# Dockerfile in a different place; context is still the current directory
docker build -t myapp -f docker/Dockerfile.prod .In this command, docker/Dockerfile.prod is the build recipe. The context is still ., so COPY src/ ./src/ in that file still reads from ./src in the directory where you ran the command. It does not read from docker/src.
How the -f path is resolved
A relative -f path is resolved against your current working directory, not against the context. The two only match when the context is ..
# Run from the repository root
docker build -t myapp -f docker/Dockerfile.prod ./appHere Docker reads docker/Dockerfile.prod from your working directory, and it sends ./app as the context. This is a common source of confusion. If the file is not found, check the path from where you are standing, not from the context.
Dockerfile outside the context
With the older classic builder, the Dockerfile had to be inside the context. BuildKit, the current default builder, accepts a Dockerfile outside the context.
docker build -t myapp -f ../shared/Dockerfile .Because behavior differs between builders, prefer a layout where the Dockerfile sits inside the context. That layout works everywhere.
The Dockerfile and .dockerignore
The builder always receives the Dockerfile, even if a .dockerignore pattern matches it. The file is used to run the build, but COPY and ADD cannot copy it into the image. The same holds for .dockerignore itself.
Reading the Dockerfile from stdin
Passing -f - makes Docker read the Dockerfile from standard input.
docker build -t myapp -f - . <<'EOF'
FROM node:20-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
CMD ["node", "server.js"]
EOFThe trailing . is still the context, so the COPY lines read from the current directory. This is handy for one-off builds and scripts.
Summary of the decision
Common errors
| Symptom | Likely cause |
|---|---|
| `unable to prepare context: unable to evaluate symlinks in Dockerfile path` | The `-f` path does not exist relative to your working directory |
| `COPY failed: file not found` after adding `-f` | The `COPY` source is relative to the context, not to the Dockerfile's folder |
| Build works on one machine, not another | One uses BuildKit and the other the classic builder, and the Dockerfile is outside the context |
Exercise
You run docker build -t api -f services/api/Dockerfile . from the repository root. The Dockerfile contains COPY shared/ ./shared/. Which directory on disk does shared/ refer to, and why?
Show answer
It refers to ./shared in the repository root, not services/api/shared. The -f flag only selects which Dockerfile to read. The context is the final argument, ., and COPY sources are resolved against the context.