Sub topic 7
How the build cache works and how to order instructions
This chapter assumes you can write a basic Dockerfile, run docker build with a context, and tag an image with -t. You should also know that an image is a stack of layers.
How the cache decides
Each instruction that changes the filesystem creates 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.
The check differs by instruction:
RUNis compared by its command text. Docker does not look at what the command downloads.RUN apt-get updatestays cached even if the package index has changed upstream.COPYandADDare compared by the content of the files being copied (checksums). Changing one byte in a copied file invalidates that step.ARGandENVchanges invalidate later steps that use the value.
The rule that drives ordering is that once one layer is invalidated, every layer after it is rebuilt. Cache hits are only possible for the unbroken run of unchanged instructions at the top of the file.
A poorly ordered Dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
CMD ["python", "app.py"]COPY . . comes before the install. Editing any source file changes the checksum of that layer. The pip install layer after it is then rebuilt, and every dependency is downloaded again on each code change.
A better ordering
Put the things that change rarely first and the things that change often last. Dependency lists change less often than source code, so copy them separately.
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]Now an edit to app.py invalidates only COPY . . and what follows. The install layer stays cached, and the rebuild takes seconds.
Watching the cache
Build twice and compare the output. Use plain progress to see every step:
docker build --progress=plain -t myapp:1.0 .
docker build --progress=plain -t myapp:1.0 .The second run should show CACHED for each step. Now change a line in app.py and build again. Only the last COPY and later steps should run.
To bypass the cache entirely, for example when a RUN step fetches something that changed upstream:
docker build --no-cache --progress=plain -t myapp:debug .Common errors
- Stale package index.
RUN apt-get updateon its own line is cached forever. Combine it with the install in oneRUNso they are invalidated together. - Cache busted by the context. A
.dockerignorethat omits.gitor logs meansCOPY . .changes on every build. Exclude files that change without affecting the app. - Cache busted early. Placing an
ARGthat changes every build, such as a timestamp or commit hash, near the top invalidates everything below it. Declare it as late as possible. - Assuming
--no-cacheis the fix. Slow builds usually come from ordering, not from a cache problem. Reorder first.
Summary of the ordering rule
- Base image and fixed setup.
- Dependency manifests, then the dependency install.
- Application source.
- Frequently changing values such as version arguments.
CMDorENTRYPOINT.
Exercise
A Dockerfile has COPY . . followed by RUN npm ci. Every source edit reinstalls all packages. How do you fix it, and which layers are rebuilt after your fix when only server.js changes?
Show answer
Copy package*.json first, run npm ci, then run COPY . .. After the change, only the COPY . . layer and the layers after it are rebuilt. The npm ci layer stays cached because the copied manifest files are unchanged.
Exercise
Why does RUN apt-get update on its own line cause problems when the cache is in use?
Show answer
Docker compares RUN steps by command text, so the layer is reused even if the package index upstream has changed. A later install can then use stale data. Combine apt-get update and the install in one RUN.