Sub topic 1
How Docker layer caching works
Prerequisites
This chapter assumes you can do the following:
- Write a basic Dockerfile and run
docker build. - Read CI workflow concepts such as jobs and runners.
- Recognize container registries and image tags.
Later chapters use AWS S3 and IAM. You don't need them here.
Layers and the build cache
Most Dockerfile instructions, such as RUN, COPY and ADD, produce a filesystem layer. A layer is a stored set of file changes on top of the layer before it. The build cache keeps these results so that BuildKit (the engine behind docker build) can skip work it has already done.
Before running a step, BuildKit asks whether it has built this exact step on top of this exact parent before. If it has, it reuses the stored result. The build output marks such a step CACHED.
What goes into a cache key
BuildKit identifies each step by a key built from:
- The parent layer's key.
- The instruction and its text. For
RUN npm ci, the command string is the input. - For
COPYandADD, a checksum of the file contents being copied. Timestamps don't count, so touching a file without changing it does not cause a miss. - Build arguments and environment values that the step uses.
BuildKit does not look inside a RUN command. If RUN apt-get update runs today and again next month, the key is the same and the old result is reused, even though the package index has changed. Rebuild without the cache, using --no-cache, when you need fresh results.
The invalidation rule
The parent layer's key is part of every key. When one step misses, every step after it misses too, even if those later instructions did not change.
This rule explains most slow builds. A single early change throws away the work of everything below it.
A cache-friendly example
Put the instructions that change rarely first and the ones that change often last. Here is a Node.js image that follows this order:
FROM node:20-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
CMD ["node", "dist/index.js"]The manifests are copied and installed before the rest of the source. When you edit only application code, the COPY package.json package-lock.json ./ checksum is unchanged. npm ci stays cached, and only COPY . . and npm run build run again. If you wrote COPY . . first, every source edit would invalidate npm ci, and you would reinstall all dependencies on every build.
Where the cache lives
On your laptop, BuildKit stores the cache on local disk, so a second build is fast. The cache belongs to that machine's builder. A fresh machine has none of it, and the next chapter covers why that matters for CI.
Exercise
A Dockerfile has these steps in order: FROM python:3.12, COPY . ., RUN pip install -r requirements.txt, CMD ["python", "app.py"]. You change one line in app.py and rebuild on the same machine. Which steps are rebuilt, and how would you reorder the Dockerfile to avoid the slowest one?
Show answer
COPY . . misses because the checksum of the copied files changed. RUN pip install and CMD then miss because their parent layer changed. The install is the slow step. Copy requirements.txt alone first, run pip install, and only then run COPY . .. A change to app.py then leaves the install cached.