Sub topic 14
BuildKit Cache Mounts for Package Managers
This chapter assumes you can read a Dockerfile, know that a changed COPY invalidates every later step, and have BuildKit available. BuildKit is the default builder in current Docker releases.
The problem that ordering does not solve
Copying requirements.txt before the source code keeps the install layer cached. That helps only while the manifest is unchanged. Once you add one dependency, the RUN pip install step is a cache miss. pip then downloads every package again, because the layer cache has no partial reuse.
A cache mount fixes this. It attaches a directory to a single RUN step and keeps that directory between builds. The directory is not part of the image, so it never becomes a layer.
A cache mount for pip
# syntax=docker/dockerfile:1
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt
COPY . .The first line selects a Dockerfile frontend that supports --mount. During the RUN step, BuildKit mounts a persistent cache at /root/.cache/pip, which is where pip stores downloaded wheels and source archives. When requirements.txt changes, the step runs again. pip finds the unchanged packages in the mounted cache and downloads only the new ones.
When the step finishes, the mount is detached. Its contents stay in BuildKit's local storage and are not in the image.
How it fits with the layer model
Two caches are involved, and they work differently.
| Property | Layer cache | Cache mount |
|---|---|---|
| Keyed by | Instruction and its inputs | The `target` path (and optional `id`) |
| Reused when | Nothing in the step or earlier steps changed | Any later build, even after a miss |
| Stored in the image | Yes, as a layer | No |
| Contents | Filesystem changes from the step | Whatever the tool writes there |
The layer cache decides whether a step runs. The cache mount makes a step that does run cheaper.
Other package managers
Each tool has its own cache directory. Mount that directory.
| Tool | Typical target |
|---|---|
| pip | `/root/.cache/pip` |
| npm | `/root/.npm` |
| apt | `/var/cache/apt` and `/var/lib/apt` |
| Go modules | `/go/pkg/mod` |
Common errors
- Using the wrong target. If the path is not where the tool caches, the mount stays empty and nothing speeds up.
- Relying on the cache for the final result. The mount is absent at runtime. Install the packages into the image normally, and let the mount hold only downloads. For example, copy a built artifact out of the mount with
cpbefore the step ends if you need it. - Expecting it in CI. A fresh CI runner has an empty mount. Cache mounts live in the builder's local storage, so they are not exported with the layer cache from the previous chapter's registry backend.
- Concurrent builds. By default, builds share the mount. Set
sharing=lockedfor tools that cannot tolerate parallel writers, such as apt.
Exercise
You add one package to requirements.txt. The install step is a layer cache miss, yet the build finishes quickly. What explains this, and where is the downloaded data stored?
Show answer
The step runs again, but the cache mount at pip's cache directory still holds the earlier downloads, so pip fetches only the new package. The data sits in BuildKit's local build storage on the builder, not in any image layer.
Exercise
Why does a cache mount not help on a brand-new CI runner, and what do you use for layer reuse there?
Show answer
The mount lives in the local storage of the builder that ran the earlier builds, and a new runner has none. For layer reuse across machines, export and import the build cache, for example with the registry cache backend.