Sub topic 5
Why one changed step rebuilds everything after it
This chapter assumes you can read a Dockerfile, run docker build, and know that each layer-creating instruction produces a layer that the build cache can reuse. Here you will see why a cache miss at one step forces a rebuild of every step after it.
The rule
Docker checks the steps of a Dockerfile from top to bottom. For each step it asks whether a cached layer exists that was built from the same parent layer with the same instruction. If the answer is yes, it reuses that layer. If the answer is no, that step runs for real.
Once one step misses, every later step misses too. A later step's cache entry is tied to its parent layer. A new parent layer means no stored entry can match, so each later step must run again.
Why the chain works this way
A layer holds only the filesystem changes made by one step. Those changes only make sense on top of a specific parent. Suppose step 3 installs packages and step 2 changed. Step 3 might now install different versions, or find different files. Docker cannot know without running it, so it assumes the worst.
For COPY and ADD, Docker also compares the contents of the copied files (checksums), not just the instruction text. For RUN, it compares only the command string. It does not check whether the command would produce a different result today.
A worked example
Take this Dockerfile, where the whole project is copied before dependencies are installed.
FROM node:20-slim
WORKDIR /app
COPY . .
RUN npm ci
RUN npm run build
CMD ["node", "dist/server.js"]Build it once, then edit any file, such as src/index.js, and build again. The output looks roughly like this:
=> CACHED [2/5] WORKDIR /app
=> [3/5] COPY . .
=> [4/5] RUN npm ci
=> [5/5] RUN npm run buildWORKDIR is a hit. COPY . . misses because the checksum of the copied files changed. npm ci has not changed as an instruction, but its parent layer is new, so it misses and reinstalls every dependency. The build step misses for the same reason.
You edited one source line and paid for a full dependency install.
Common errors
- Assuming an unchanged instruction stays cached. The instruction text is only part of the cache key. The parent layer matters just as much.
- Expecting
RUN apt-get updateto refresh. If its text and parent are unchanged, Docker reuses the old result, so you can get a stale package index. Combiningupdateandinstallin oneRUNkeeps them together. - Blaming the wrong step. When a build is slow, find the first step that is not marked
CACHED. That step is the cause. Everything below it is a consequence. - Changing early metadata. An
ARGorENVwith a new value near the top can invalidate the steps that use it, and the steps after those too.
The next chapter uses this rule in reverse. If a miss spreads downward, you can place rarely changing steps first and frequently changing steps last.
Exercise
A Dockerfile has these steps in order: FROM, RUN apt-get install -y git, COPY requirements.txt ., RUN pip install -r requirements.txt, COPY . . You change only requirements.txt. Which steps are rebuilt?
Show answer
The FROM and apt-get install steps are cache hits. The COPY requirements.txt step misses because the file contents changed. The pip install step and the final COPY . . step also miss, because each follows a changed layer.