Sub topic 14
BuildKit cache and secret mounts
This chapter assumes you can write a Dockerfile, build and run an image, and understand layers and the build cache. BuildKit is the build engine behind docker build in current Docker releases. It adds mounts that last only for a single RUN instruction. This chapter covers two of them: cache mounts and secret mounts.
Why layers alone fall short
Layer caching has two weaknesses.
- If
package.jsonchanges, thenpm cilayer is rebuilt from scratch. Every package is downloaded again, even if only one dependency changed. - Anything a
RUNinstruction writes stays in that layer. If you copy a token into the image to install a private package, the token remains in the image history, even if a later step deletes it.
Cache mounts address the first problem. Secret mounts address the second.
Cache mounts
A cache mount attaches a directory that persists between builds. It is not part of the image. Use it for package manager download caches.
# syntax=docker/dockerfile:1
FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN --mount=type=cache,target=/root/.npm \
npm ci --omit=dev
COPY . .
USER node
CMD ["node", "server.js"]The # syntax= line on the first line selects a current Dockerfile frontend. It is needed on older setups and harmless otherwise.
When the npm ci layer is invalidated, npm still finds its downloaded packages in /root/.npm. The layer is rebuilt, but the download step is mostly skipped. The cache contents are never written into the image, so the image size does not grow.
Common cache targets:
| Tool | Cache directory |
|---|---|
| npm | `/root/.npm` |
| pip | `/root/.cache/pip` |
| Go modules | `/go/pkg/mod` |
| Go build cache | `/root/.cache/go-build` |
| apt | `/var/cache/apt` |
Secret mounts
A secret mount makes a file available at /run/secrets/<id> while one RUN instruction executes. The file is not stored in any layer.
# syntax=docker/dockerfile:1
FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc \
npm ci --omit=dev
COPY . .
USER node
CMD ["node", "server.js"]Pass the secret when you build:
docker build --secret id=npmrc,src=$HOME/.npmrc -t myapp:1.0 .The id in the Dockerfile must match the id on the command line. With target, the file appears at that path. Without it, it appears at /run/secrets/npmrc.
Common errors
- The mount is empty or the build fails. The
idin--mountdoes not match--secret id=.... - Cache mounts have no effect. The build is running on a different builder or machine, or the cache was pruned. Cache mounts are stored on the builder, not in the image.
- A cached file is missing from the final image. This is expected. Contents of a cache mount are not in the image. Copy anything you need into a normal path within the same
RUNinstruction. unknown flag: --mount. The builder is not using BuildKit, or the frontend is too old. Add the# syntax=line and use a current Docker version.- Secret still ends up in the image. The
RUNcommand copied it elsewhere, for example into/app. Keep the secret at its mount path.
When to use these features
Use cache mounts when dependency installation is slow and the same builder runs repeated builds. They matter less in CI systems that start with a clean builder each time, unless you configure external cache storage.
Use secret mounts any time a build step needs a credential, such as a private registry token or an SSH key.
Exercise
A Dockerfile contains ARG NPM_TOKEN and a RUN step that writes the token to .npmrc, runs npm ci, then deletes .npmrc. Why is this unsafe, and what replaces it?
Show answer
The ARG value is recorded in the image's build history, and the .npmrc written by RUN stays in that layer even after the later deletion. Use RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci and build with --secret id=npmrc,src=$HOME/.npmrc, so the credential is never stored in a layer.
Exercise
You add RUN --mount=type=cache,target=/root/.npm npm ci and the final image size does not change. Is that expected, and why?
Show answer
Yes. The cache mount lives on the builder and is excluded from the image layers. It speeds up rebuilds by keeping downloaded packages, not by changing what the image contains.