Sub topic 6
Cache mount recipes for apt, pip, npm and Go
This chapter assumes you can write a multi-stage Dockerfile and that you know the RUN --mount=type=cache,target=... syntax from earlier chapters. Here you apply it to four common tools. Each recipe needs two facts: the directory where the tool keeps its downloads, and any setting that stops the tool from using that directory.
Find the cache directory
A cache mount only helps if target matches the directory the tool actually reads and writes. The paths below assume the build runs as root.
| Tool | Cache directory | Command it speeds up |
|---|---|---|
| pip | `/root/.cache/pip` | `pip install` |
| npm | `/root/.npm` | `npm ci` |
| Go modules | `/go/pkg/mod` | `go mod download`, `go build` |
| Go build cache | `/root/.cache/go-build` | `go build` |
| apt | `/var/cache/apt` and `/var/lib/apt` | `apt-get update`, `apt-get install` |
If a tool prints its cache location, use that value. For example, pip cache dir and go env GOCACHE both do.
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 . .
CMD ["python", "main.py"]When requirements.txt changes, the RUN layer is invalidated and runs again. pip still finds the wheels it downloaded earlier in the mounted cache, so only new or changed packages are fetched.
npm
# syntax=docker/dockerfile:1
FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN --mount=type=cache,target=/root/.npm \
npm ci
COPY . .Use npm ci here. It installs exactly what package-lock.json lists, and it reuses tarballs from /root/.npm. The node_modules directory is not a cache. It stays in the image layer because the application needs it at run time.
Go
Go keeps two caches, so mount both in the build step.
# 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 saves downloads. The build cache stores compiled packages, so after a small source change Go recompiles only the packages that changed. Write the output binary to a path outside both mounts, such as /out. Anything written inside a cache mount is not part of the image.
apt
apt needs more setup. Debian-based images ship a docker-clean hook that deletes downloaded packages after every install, which leaves the cache empty. Remove the hook and tell apt to keep downloads.
# syntax=docker/dockerfile:1
FROM debian:bookworm
RUN rm -f /etc/apt/apt.conf.d/docker-clean; \
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
apt-get update && apt-get install -y --no-install-recommends curlTwo details matter here:
/var/cache/aptholds the.debfiles and/var/lib/aptholds the package lists, so both are mounted.sharing=lockedmakes concurrent builds take turns. apt takes its own lock and does not tolerate two processes using one cache.
When the cache does not help
- Set
PIP_NO_CACHE_DIR=1or runpip install --no-cache-dirand pip skips the mounted directory. Remove those settings when you use a cache mount. - Running the build as a non-root user changes the home directory. Point
targetat that user's cache path and setuidandgidon the mount. - The first build on a fresh machine is still slow, because the cache starts empty.
Exercise
A Dockerfile mounts a cache at /var/cache/apt, but every build still downloads every package. The base image is debian:bookworm. What is the likely cause, and what fixes it?
Show answer
The docker-clean hook in /etc/apt/apt.conf.d deletes downloaded .deb files after each install, so the cache stays empty. Remove /etc/apt/apt.conf.d/docker-clean and set Binary::apt::APT::Keep-Downloaded-Packages "true" in an apt config file. Also mount /var/lib/apt and use sharing=locked.