Sub topic 2
Why layer caching is not enough
This chapter assumes you can write a basic Dockerfile with FROM, COPY and RUN, and that you run docker build from the command line. It also assumes you know that each instruction produces an image layer, and that you have used a package manager such as pip, npm or apt. Later chapters introduce RUN --mount. This chapter explains the problem it solves.
How layer caching works
Docker caches the result of each instruction. On the next build, it compares each instruction with the cached one. For COPY and ADD, it also compares the checksums of the files being copied. If everything matches, Docker reuses the cached layer and skips the work.
The rule that causes trouble is this: once one layer is invalidated, every layer after it is rebuilt. Docker does not try to reuse later layers once an earlier one has changed.
The standard optimization
The usual advice is to copy the dependency file first and the source code afterward:
# syntax=docker/dockerfile:1
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "main.py"]If you edit main.py, only the layers from COPY . . onward are rebuilt. The pip install layer comes from the cache, so the build takes seconds.
Where it breaks down
The optimization only helps when requirements.txt stays the same. When you add or upgrade one package, the COPY requirements.txt . layer is invalidated. The pip install layer is invalidated with it, because its input changed.
Docker then reruns pip install -r requirements.txt from scratch. Pip downloads every package again, including the 40 packages that did not change. Nothing from the previous run is available, because the download cache lived inside a layer that has just been discarded.
The same happens with other tools:
| Tool | What is repeated after a one-line dependency change |
|---|---|
| pip | Download and install of every package |
| npm | Download of every package in `node_modules` |
| apt | `apt-get update` and download of every `.deb` file |
| Go | Download of modules and recompilation of all packages |
For a project with large dependencies, such as a machine-learning stack, this can add several minutes to a build for a change of one line.
Why a layer is the wrong unit
A layer is all or nothing. Docker can either reuse the whole layer or rerun the whole command. It has no way to say "most of this work is still valid". Package managers do have a notion of partial reuse. They keep a cache directory of downloaded packages, and they skip anything already there.
Two things stop you from using that directory today.
- The cache is inside the discarded layer. When the layer is rebuilt, the previous cache directory is not part of the new filesystem.
- Keeping it in the image is a poor fix. If you leave the cache in the layer, the image becomes larger. If you delete it in the same
RUNcommand, as inpip install --no-cache-dir, the image stays small, but you have given up on reuse completely.
# syntax=docker/dockerfile:1
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txtThis is the common trade-off. You choose a small image or a fast rebuild, but not both.
What is needed instead
Look at what you want. The cache directory should survive between builds, but it should not become part of the image. That means it must live outside the layers, in storage that BuildKit manages on the build host and attaches to a RUN step only while the command runs.
That is what a cache mount does. The next chapters show the syntax. This chapter's point is that layer caching decides whether a whole step reruns, and a cache mount decides how much work the step does when it reruns.
Exercise
A Dockerfile copies requirements.txt, runs pip install, then copies the source. You change one line in main.py and rebuild. Then you add one package to requirements.txt and rebuild. For each build, say whether the pip install step runs, and whether pip re-downloads unchanged packages.
Show answer
After the main.py change, the pip install layer is reused from the cache because requirements.txt did not change, so nothing is downloaded. After the requirements.txt change, the pip install layer is invalidated and runs again. Pip re-downloads every package, including unchanged ones, because the previous download cache was stored in the discarded layer or removed from the image.
Exercise
Why does adding --no-cache-dir to pip install not speed up rebuilds when requirements.txt changes?
Show answer
The flag only stops pip from writing its download cache into the layer. It reduces image size but leaves no cache for the next build, so every package is downloaded again whenever the install layer is invalidated.