Sub topic 4
Speeding up builds with cache mounts
This chapter assumes you can write a basic Dockerfile, run docker build, and understand that each instruction creates a cached image layer. It also assumes you have BuildKit enabled with the # syntax=docker/dockerfile:1 line at the top of the file, as covered earlier.
The problem a cache mount solves
Layer caching reuses a layer only when its instruction and inputs are unchanged. Suppose requirements.txt gains one new package. The COPY requirements.txt . layer changes, so the RUN pip install -r requirements.txt layer is rebuilt from scratch. Pip downloads every package again, even though only one is new.
A cache mount fixes this. BuildKit attaches a persistent directory to a single RUN step. The tool in that step, such as pip, reads and writes its download cache there. When the step finishes, the directory is detached. Its contents are not part of the resulting layer, so the image does not grow.
The basic form
Add --mount=type=cache,target=<path> to a RUN instruction. The target is the directory the tool uses as its cache.
# 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 . .
CMD ["python", "main.py"]Two things happen here:
- If
requirements.txtchanges, theRUNstep runs again, but pip finds earlier downloads in/root/.cache/pipand skips them. - If nothing changes, the normal layer cache still skips the whole step. The cache mount only helps when the step actually runs.
How the pieces fit together
The diagram shows what happens when the step re-runs.
Cached files are not in the image
Because the mount is not part of the layer, the cache directory is empty in the final image. This has a practical consequence. A later instruction cannot rely on files that exist only in the mount.
For example, this fails to preserve anything useful:
# syntax=docker/dockerfile:1
FROM python:3.12-slim
RUN --mount=type=cache,target=/opt/data \
echo "hello" > /opt/data/note.txt
RUN cat /opt/data/note.txtThe second RUN has no mount, so /opt/data/note.txt does not exist and cat fails. Use cache mounts only for data that a tool can regenerate. Anything the final image needs must be written outside the mount.
Go: a compiler example
Go benefits from two caches: downloaded modules and compiled packages.
# syntax=docker/dockerfile:1
FROM golang:1.22 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download
COPY . .
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go build -o /out/app ./cmd/appThe module cache holds downloaded dependencies. The build cache at /root/.cache/go-build holds compiled objects, so only changed packages are recompiled. The binary goes to /out/app, which is outside any mount and therefore stays in the layer.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `unknown flag: mount` | BuildKit is not in use or the syntax line is missing | Add `# syntax=docker/dockerfile:1` as the first line and use a current Docker version |
| Packages download again on every build | `target` does not match the tool's real cache path | Check the tool's documentation for its cache directory |
| A file written in the mount is missing later | Mount contents are not saved in the layer | Write needed output to a normal path |
| Cache never helps in CI | The CI builder starts clean each run | Covered in the CI chapter |
Choosing the target path
The target must match where the tool actually caches. Common locations are /root/.cache/pip for pip, /root/.npm for npm, and /go/pkg/mod for Go modules. If the image runs as a non-root user, the home directory differs, so adjust the path accordingly.
Exercise
A build's pip step re-runs because requirements.txt changed. You added a cache mount at /root/.cache/pip, yet a colleague says the final image does not contain the pip cache. Is this a bug, and why?
Show answer
It is not a bug. Cache mount contents are attached only while the RUN step executes and are not written into the layer. This keeps the image small while still letting pip reuse earlier downloads on later builds.