Sub topic 2
Dockerfile instructions and the build context
This chapter assumes you can run containers with docker run, read a simple Dockerfile, and have built a small app in Go, Node.js or Python. It covers the instructions you will use in every later chapter, and the build context, which decides what files a build can see.
The instructions that matter for size
A Dockerfile is a list of instructions that Docker runs in order to produce an image. Each instruction does one job.
| Instruction | What it does | Adds a filesystem layer? |
|---|---|---|
| `FROM` | Sets the base image and starts a build stage | Starts from the base's layers |
| `WORKDIR` | Sets the working directory for later instructions | No file changes, except it creates the directory if missing |
| `COPY` | Copies files from the build context (or another stage) into the image | Yes |
| `RUN` | Runs a command at build time and keeps the filesystem changes | Yes |
| `ENV` | Sets an environment variable in the image | No |
| `USER` | Sets the user for later instructions and for the container | No |
| `ENTRYPOINT` / `CMD` | Define the command the container runs at start | No |
Only RUN, COPY and ADD change files, so those are the instructions that make an image larger. The others only record settings in the image's metadata.
Use COPY rather than ADD for ordinary files. ADD also unpacks archives and fetches URLs, which is rarely what you want.
RUN executes at build time. CMD and ENTRYPOINT execute only when a container starts. A common mistake is putting a build step in CMD, which makes every container start repeat that work.
What the build context is
When you run docker build -t myapp ., the final . is the build context. It is the directory whose contents Docker makes available to the build. The Dockerfile can only COPY files that are inside this context. A path such as COPY ../secrets.txt . fails, because it points outside it.
Before the first instruction runs, the Docker client sends the context to the builder. With BuildKit, the default builder in current Docker, only the files that your instructions need are transferred. Even so, a large context has real costs:
- Large directories such as
node_modulesor.gitslow down the transfer, especially with a remote builder. COPY . .copies everything in the context. Any file that changes, even a log file, changes the layer.- Files you never meant to ship, such as
.env, can end up inside the image.
The next two chapters build on this. Layer caching depends on which files COPY sees, and .dockerignore controls what the context contains.
A worked example
Here is a small Node.js Dockerfile. The comments describe what each line does to the image.
FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
ENV NODE_ENV=production
USER node
CMD ["node", "index.js"]FROMpulls the base image layers.WORKDIRcreates/appand makes it the current directory.COPY package*.json ./copies only the dependency manifests from the context into/app.RUN npm ci --omit=devinstalls production dependencies. This is usually the largest layer you add.COPY . .copies the rest of the context, which includes your source code.ENV,USERandCMDrecord metadata only.
The two COPY steps are separate on purpose. Chapter 3 explains why.
Common errors
COPY failed: file not found in build context. The path is wrong, or.dockerignoreexcludes the file. Check both.- A slow "Sending build context" step. The context holds large directories. Build from a smaller directory or exclude them.
- Wrong context directory. The Dockerfile can live elsewhere (
docker build -f docker/Dockerfile .), but the context is still the directory you pass at the end.
Exercise
You run docker build -f build/Dockerfile build/ in a project where src/ sits next to build/. The Dockerfile contains COPY src/ /app/src/. Does the build succeed, and why?
Show answer
No. The context is build/, because that is the directory passed at the end of the command. src/ is outside it, so COPY cannot find it. Fix it by running docker build -f build/Dockerfile . from the project root so the context includes src/.