Building images
How you build decides whether a deploy takes ten seconds or ten minutes: control the context and respect the cache.
? What do the diagram colours mean
key idea / Docker itselfcontainer / good pathhost / watch outproblem / blockedneutral
The build context
The dot in docker build . is the context: the folder that is uploaded to the daemon.
The layer cache
Docker reuses any layer whose inputs did not change.
Multi-stage builds
The tools you need to build an app (compilers, dev dependencies, source) are not the tools you need to run it. A multi-stage Dockerfile builds in one stage and copies only the result into a small final stage.
FROM node:22 AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM nginx:1.27-alpine
COPY --from=build /app/dist /usr/share/nginx/htmlThe same pattern works for any compiled language: build a Go binary in golang:1.23, then copy just the binary into alpine or gcr.io/distroless/static. Smaller images pull faster and contain fewer packages to patch. Chapter 18 takes this further.
Flags worth knowing
| Flag or file | Effect |
|---|---|
-t name:1.0 | tag the image so you can find and run it |
.dockerignore | filters the context: node_modules, .git, build output |
--no-cache | ignore the cache, rebuild every layer |
--target build | stop at a named stage, for example to run tests in it |
-f path/Dockerfile | use a Dockerfile that is not named Dockerfile |
Try it yourself
Stay in the greet directory from the previous chapter, or recreate that Dockerfile first.
-
Step 1 · See the context get sent
A large unused file is still part of
.until you ignore it.$ dd if=/dev/zero of=big.bin bs=1m count=20 $ docker build -t greet:1 . # transferring context: 20MB -
Step 2 · Ignore it
$ printf 'big.bin\n' > .dockerignore $ docker build -t greet:1 . # transferring context: a few kB -
Step 3 · Change a source file and watch the cache
FROMandWORKDIRare reused.COPYand everything after it run again, becausehello.txtchanged.$ printf 'hello again\n' > hello.txt $ docker build -t greet:1 . [1/4] FROM docker.io/library/alpine:3.20 CACHED [2/4] WORKDIR /app CACHED [3/4] COPY hello.txt . [4/4] RUN printf 'built in the image\n' > /app/built.txt -
Step 4 · --no-cache rebuilds every layer
$ docker build --no-cache -t greet:1 . [1/4] FROM docker.io/library/alpine:3.20 [2/4] WORKDIR /app [3/4] COPY hello.txt . [4/4] RUN printf 'built in the image\n' > /app/built.txt -
Step 5 · A two-stage build
Stage one installs a full compiler toolchain; stage two copies only the result into a fresh base.
$ cat > Dockerfile.multi << 'EOF' FROM alpine:3.20 AS build RUN apk add --no-cache build-base && echo 'compiled!' > /out.txt FROM alpine:3.20 COPY --from=build /out.txt /out.txt CMD ["cat", "/out.txt"] EOF $ docker build -f Dockerfile.multi -t multi:1 . $ docker run --rm multi:1 compiled! $ docker images multi REPOSITORY TAG SIZE multi 1 7.8MBThe compiler (over 200 MB) lived in stage one only. The final image is the size of plain Alpine.
.dockerignore. COPY . . then sends node_modules, .git and your .env into the build: slow builds, large images, and secrets saved in a layer.Recap
- The build context is uploaded to the daemon, so keep it small with
.dockerignore. - A layer is rebuilt when its inputs change, and so is every layer after it.
- Copy dependency files first, install, then copy your source.
--no-cacheforces a clean rebuild.- A multi-stage build compiles in one stage and ships only the result from a small final stage.
Quick check
You edit one source file and the build reinstalls every npm package. What is likely wrong?
Show answer
Answer: The Dockerfile copies all the source before installing dependencies.
Copy package*.json, run the install, then copy the source, so the install layer stays cached when only source changes.
In a multi-stage build, what ends up in the final image?
Show answer
Answer: Only the last stage, plus anything it copies with COPY --from.
Only the final stage becomes the image. Earlier stages exist to produce files that COPY --from brings in.