Sub topic 6
Using the build cache well
This chapter assumes you can write a basic Dockerfile, run docker build, and know that an image is a stack of layers. It covers how Docker decides to reuse a layer, and how to order instructions so rebuilds stay fast.
How the cache decides
Each instruction in a Dockerfile produces a layer. Before running an instruction, Docker checks whether it already has a layer built from the same parent layer and the same instruction. If so, it reuses that layer and prints CACHED.
For COPY and ADD, Docker also compares the checksums of the files being copied. If any file changed, the cache misses for that instruction.
One rule drives everything else. Once one instruction misses the cache, every instruction after it runs again.
Order from stable to volatile
Put the things that change rarely at the top and the things that change often at the bottom. Source code changes on almost every build. Dependency manifests change much less often.
The Dockerfile below copies only the dependency files first, so the download layer stays cached when you edit code.
FROM golang:1.22 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app ./cmd/app
FROM gcr.io/distroless/static-debian12
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]If you edit a .go file, the build reuses everything through RUN go mod download. It reruns only COPY . . and the build. If you had written COPY . . first, any edit would invalidate the download step too.
Common ways the cache breaks
- A noisy build context.
COPY . .includes every file in the context. A changing log file or the.gitdirectory invalidates the layer. Exclude them with a.dockerignorefile. - Commands with changing output.
RUN apt-get updateis cached by its text, not by what the package index contains. The layer can go stale and keep old package lists. Combine it with the install in oneRUNso they are cached together. - Early
ARGvalues. Changing a build argument invalidates every instruction that uses it, and everything after. - Timestamps and generated files. Anything that writes the current time into copied files changes the checksum on every build.
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/*Checking and bypassing the cache
Build twice and watch the output. The second run of an unchanged build should show CACHED for each step. docker history myapp:1.0 shows the layers and their sizes.
To force a full rebuild, for example to pick up security updates in a base image, use:
docker build --no-cache --pull -t myapp:1.0 .--pull also fetches the newest version of the base image tag.
Exercise
A Dockerfile runs COPY . . and then RUN npm install. Every code edit reinstalls all dependencies. How do you fix it?
Show answer
Copy only package.json and the lock file first, run npm install, and then run COPY . .. The install layer is then reused until the dependency files change.