Sub topic 4
How the build cache decides on a hit or a miss
This chapter assumes you can read a Dockerfile and run docker build. It also assumes you know that each layer-creating instruction produces a layer, as covered earlier in this tutorial.
What the cache stores
Docker builds an image one instruction at a time. After each step, the builder keeps the result. On the next build, it checks each instruction against what it kept. If the step matches, the builder reuses the stored result. This is a cache hit. If nothing matches, it runs the step again. This is a cache miss.
The check for each instruction
For every instruction, the builder compares two things: the state before the step, and the step itself. What counts as "the step itself" depends on the instruction type.
| Instruction | What the cache compares |
|---|---|
| `RUN` | The command text as written. The builder does not look at what the command would produce. |
| `COPY` and `ADD` | The content checksums of the files copied. Modification times are ignored. |
| `ENV`, `WORKDIR`, `CMD` | The instruction text. |
| `ARG` | The instruction text, plus the value passed with `--build-arg` when a later `RUN` uses it. |
The state before the step also has to match. If an earlier step missed, the later steps are rebuilt too. The next chapter covers that cascade.
Seeing hits and misses
Take this Dockerfile.
FROM node:20-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
CMD ["node", "dist/server.js"]Build it twice with no changes. BuildKit marks reused steps as CACHED in the output.
docker build -t myapp:dev .
docker build -t myapp:dev .On the second run, every step shows CACHED. Now edit one source file and build again. The two COPY steps and the RUN steps behave differently:
COPY package.json package-lock.json ./hits, because those two files have the same content.RUN npm cihits, because its text and its parent state are unchanged.COPY . .misses, because a file's checksum changed.RUN npm run buildruns again, because the step before it missed.
Common errors
The RUN rule causes the most surprises. The builder compares the command text, so it cannot see changes outside the Dockerfile. Consider this line:
RUN apt-get update && apt-get install -y --no-install-recommends curlIf the text never changes, the builder reuses the old result. That result contains a package index from the day of the first build. New package versions will not appear until something forces a rebuild.
Another common error is touching a file and expecting a rebuild. Since COPY compares content, touch file does not invalidate anything. A real edit does.
Summary of the rule
A step is a hit only when its parent state matches and its own input matches. For RUN, the input is the command string. For COPY and ADD, the input is file content. Anything the command reads from the network or the host is invisible to the cache.
Exercise
Your Dockerfile has RUN curl -fsSL https://example.com/tool.sh | sh. The script at that URL is updated, but your rebuild shows CACHED for this step. Why, and how do you force a fresh run?
Show answer
The cache compares the command text, and the text did not change. The builder cannot see that the downloaded script changed. Rebuild with docker build --no-cache ., or change the command text, for example by pinning a version in the URL.
Exercise
You run touch src/app.js without editing it and rebuild. Will COPY . . be a cache hit?
Show answer
Yes, as long as no other file in the copied set changed. COPY compares content checksums and ignores modification times.