Sub topic 4
How the Build Cache Decides on a Hit or a Miss
This chapter explains the rules Docker uses to reuse a layer or rebuild it. It assumes you can run docker build, read a simple Dockerfile, and know that an image is a stack of layers. It covers the cache key for each instruction, how COPY and ADD are checked, why RUN is not, and the errors that follow from these rules.
The cache is a lookup, not a diff
Docker does not compare your new Dockerfile with the last one. For each instruction it asks one question: "Have I already built a result for exactly this step, on top of exactly this parent?" If the answer is yes, the build reports a cache hit and reuses the stored result. If the answer is no, it reports a miss and runs the step.
A step is identified by a cache key. The key has two parts:
- The parent. This is the result of the previous step, or the base image for the first step.
- The instruction itself, plus any inputs that instruction reads.
Because the parent is part of the key, a result is only reused when everything beneath it is also identical. The next chapter covers what that means for later steps. Here we focus on how the second part, the instruction and its inputs, is checked.
What is compared for each instruction
The inputs differ by instruction type.
| Instruction | What the cache compares |
|---|---|
| `RUN` | The command string, the parent, and any build arguments that the command uses |
| `COPY`, `ADD` (local files) | The destination, the options, and a checksum of the content of every source file |
| `ADD` (remote URL) | The URL. With BuildKit, a changed `--checksum` also counts |
| `ENV`, `WORKDIR`, `USER`, `LABEL`, `CMD`, `ENTRYPOINT` | The instruction text and the parent |
| `ARG` | The value. A different value invalidates steps that use it |
The most important row is the first one. For RUN, Docker compares the command text and nothing else.
RUN is checked by its text, not its effect
Docker does not look at what a RUN command does or what the network would return today. It only checks whether the string is the same as before.
FROM debian:bookworm-slim
RUN apt-get update
RUN apt-get install -y --no-install-recommends curlSuppose you build this today. Three weeks later the package index has changed, but the text RUN apt-get update has not. Docker reports a cache hit and gives you the old index. The install step then has a different text only if you edit it. If you add a new package to the second line, that line misses, but the index from the first line is still three weeks old. The install can then fail with "package not found" or install an out-of-date version.
The fix is to run the update and the install in one RUN, so they share one cache entry:
FROM debian:bookworm-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/*Now any change to the package list changes the command text. The whole step misses and the index is fetched again. Cleaning up in the same step also keeps the apt lists out of the layer, which a later chapter explains.
COPY and ADD are checked by content
For COPY and ADD with local files, Docker computes a checksum over the files that match the source paths. The checksum covers file contents and some metadata such as the file mode. It does not use the modification time. This has two consequences:
- Running
touchon a file does not cause a miss. - Changing a single byte in one source file does cause a miss for that step and for every step after it.
A common pattern relies on this rule. In this Dockerfile the install step is only repeated when the manifests change:
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"]Walk through two builds:
- You edit
src/server.jsand rebuild. TheFROM,WORKDIR, firstCOPYandRUN npm cisteps are all hits. The checksums ofpackage.jsonandpackage-lock.jsonhave not changed. TheCOPY . .step is a miss, because one of its source files changed.npm run buildthen runs again as a consequence. - You add a dependency and rebuild. The first
COPYis now a miss, sonpm ciruns again.
When you run a build with plain output, the hit and miss status is visible:
docker build --progress=plain -t myapp:dev .Steps that were reused are marked CACHED. Steps that ran show their output and a duration.
Subtle cases
The build context is the input to COPY
COPY . . takes its checksum from the build context, which is the directory you pass to docker build. Any file in the context that matches the source path is part of the check. A log file, a .git directory, or an editor swap file that changes on every build makes COPY . . miss every time. A .dockerignore file removes those files from the context. A later chapter covers it in detail. For now, remember that the cache is only as stable as the files you copy.
Time and the network are invisible
Docker has no idea that a command depends on the clock or the network. These commands are hits when the text is unchanged:
FROM alpine:3.20
RUN date > /build-time.txt
RUN wget -q -O /latest.json https://example.com/api/latestThe first build records one date and one download. Later builds reuse both and never refresh them. If you need a fresh result, you must change something Docker can see. Options include:
- Passing a build argument that you change on purpose, such as
--build-arg CACHE_BUST=2026-12-04, and using it in the command. - Building with
--no-cacheto ignore the cache for every step. - Building with
--pullto check for a newer base image.
Base images are checked by digest, not by tag
The first step's parent is the base image. A tag such as node:20-slim is a movable name. If you have a local copy, Docker uses it as the parent without contacting the registry, unless you pass --pull. Two machines can have different images under the same tag, so they will have different caches, and a hit on one machine says nothing about the other. Pinning the base image by digest makes the parent exact:
FROM node:20-slim@sha256:0000000000000000000000000000000000000000000000000000000000000000The digest above is a placeholder. Replace it with the real value from docker image inspect or your registry. Digests are explained in a later chapter.
Build arguments and environment variables
An ARG is part of the cache key for any later RUN that can see it. With the classic builder, a changed ARG value invalidates every RUN after the ARG line, because the value is available in their environment. BuildKit narrows this to the steps that actually use the value, but you should still declare an ARG as late as possible. An ENV value is stored in the image and is part of the parent for every step after it.
Common mistakes
- Expecting a hit after editing a comment inside a file you copy. The checksum covers file content, so any edit is a miss. A comment in the Dockerfile itself is different. Comment lines are not instructions and do not change the cache key in most cases.
- Splitting
apt-get updatefromapt-get install. This creates the stale index problem shown above. - Copying the whole project before installing dependencies. Any change to any file then re-runs the install step.
- Assuming a hit means the result is current. A hit only means the inputs Docker can see are unchanged.
- Comparing timings between machines. Each builder keeps its own cache, so a hit on your laptop does not mean a hit in CI. A later chapter covers sharing the cache.
Summary of the rules
- A step hits only if its parent hit or is identical, and its own key matches.
RUNis keyed by command text. Docker does not see what the command reads.COPYandADDare keyed by file content, not timestamps.- Anything Docker cannot see, such as the clock, a package index, or a remote API, can go stale inside a hit.
- To force a refresh, change a visible input or disable the cache.
Exercise
A Dockerfile has "RUN apt-get update" on one line and "RUN apt-get install -y curl" on the next. Three weeks after the first build, you change the second line to install "curl jq" and rebuild. Which of the two steps is a cache hit, and what problem can this cause?
Show answer
The first step, "RUN apt-get update", is a hit because its command text is unchanged, so Docker reuses the three-week-old package index. The second step is a miss because its text changed, so it runs against the stale index. The install can fail with "package not found" or fetch outdated versions. Combining both commands in one RUN, joined with "&&", makes any change to the package list re-run the update too.
Exercise
You run "touch src/app.js" without changing the file's content and rebuild a Dockerfile that has "COPY . .". Is the COPY step a hit or a miss, and why?
Show answer
It is a hit, assuming no other file in the context changed. Docker checksums the file content and certain metadata such as the mode, not the modification time, so touch does not change the cache key.