Sub topic 12
Faster builds with BuildKit cache mounts
This chapter assumes you know how layer caching works, how multi-stage builds are written, and how to read a Dockerfile for Node.js or Python. It covers one feature: BuildKit cache mounts.
The problem with layer caching
Layer caching reuses a layer only when its instruction and inputs are unchanged. Suppose you change one line in requirements.txt. The RUN pip install layer is invalidated, and pip downloads every package again, including the ones that did not change.
A cache mount fixes this. It attaches a persistent directory to a single RUN instruction. The directory lives in BuildKit's own storage, outside the image, so it survives between builds and adds nothing to the image size.
How a cache mount works
You add a --mount=type=cache flag to a RUN instruction and name the directory to keep with target.
Two properties matter:
- The mounted directory persists across builds on the same builder. A second build finds the files the first one downloaded.
- The directory is not in the layer. Anything you write there during the
RUNstep is not in the final image.
Example: pip
This is the build stage from the Python example, shown with the syntax line it needs.
# syntax=docker/dockerfile:1
FROM python:3.12-slim AS build
WORKDIR /app
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
COPY requirements.txt .
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txtThe first line, # syntax=docker/dockerfile:1, selects a current Dockerfile frontend. Current Docker releases use BuildKit by default. On an old installation, set DOCKER_BUILDKIT=1 before running docker build.
pip stores downloaded wheels and source archives in /root/.cache/pip. Because that path is a cache mount, a changed requirements.txt only downloads the packages that are new or changed. Without the mount, you would have to pass --no-cache-dir to keep that directory out of the image. The mount makes that unnecessary, because the directory is never written into a layer.
Other package managers
The pattern is the same everywhere. Find the directory where the tool keeps its downloads, then mount it.
| Tool | Cache directory (typical) | Example mount target |
|---|---|---|
| pip | `/root/.cache/pip` | `/root/.cache/pip` |
| npm | `/root/.npm` | `/root/.npm` |
| Go modules | `/go/pkg/mod` | `/go/pkg/mod` |
| apt | `/var/cache/apt` | `/var/cache/apt` |
For npm, the instruction becomes:
RUN --mount=type=cache,target=/root/.npm npm ciCheck the actual location for your tool and base image. Running as a different user or setting a custom cache variable changes the path.
Limits and gotchas
- The cache belongs to the builder. A fresh CI runner starts with an empty cache, so you gain nothing unless the runner keeps BuildKit's storage between jobs or you export the cache.
- A cache mount speeds up work inside one
RUNstep. It does not stop Docker from rerunning that step when its inputs change. - Do not rely on the mounted files being there at runtime. The final image never contains them.
- Other people building on the same builder can share the cache. For untrusted builds, treat the cache as shared state.
Checking the effect
Build twice, changing one dependency between runs, and compare the time of the install step.
docker build -t myapp:small .
# edit requirements.txt, then:
docker build --progress=plain -t myapp:small .With --progress=plain, the output shows pip printing Using cached for packages it already had.
Exercise
A teammate adds --mount=type=cache,target=/root/.cache/pip to the pip install step and then checks the final image for the cached wheels. They find none. Is this a bug, and why does the next build still get faster?
Show answer
It is not a bug. A cache mount lives in BuildKit's storage and is never written into the image layer, so the final image does not contain the wheels. The next build is faster because the same builder still holds the mounted directory, and pip reuses the downloads that are already there.