Sub topic 13
CI behavior and common pitfalls
This chapter assumes you can write multi-stage Dockerfiles and have used RUN --mount=type=cache and RUN --mount=type=secret in local builds. Here you will see how those mounts behave on CI runners and which mistakes cause most of the trouble.
Cache mounts do not follow your build to CI
A cache mount lives in the BuildKit builder's own storage, not in the image. On your laptop the builder is long-lived, so the cache survives between builds. Many CI systems start a fresh virtual machine or container for every job. A new builder starts with empty cache mounts, so every build looks like a cold build.
You have three common ways to deal with this:
- Use a persistent builder, such as a self-hosted runner with a long-lived BuildKit daemon.
- Export and import the layer cache with
--cache-toand--cache-from. This stores layer results in a registry or in a CI cache backend. - Restore the cache directory yourself with a CI cache step and inject it into the build.
Pitfalls with cache mounts
Debian images delete downloaded packages. The base image ships an docker-clean hook that removes .deb files after each apt-get run. A cache mount on /var/cache/apt then stays empty. Remove the hook and tell apt to keep downloads, as shown in the apt recipe earlier.
Concurrent builds corrupt shared caches. Package managers such as apt use lock files. Two builds that write to the same mount at once can break it. Use sharing=locked for apt. The default shared mode is fine for caches that tolerate parallel use, such as pip.
The cache is not in the final image. The mount is detached after the RUN step ends. If a later step needs a file from the cache, for example a compiled binary, copy it out inside the same RUN:
# syntax=docker/dockerfile:1
FROM golang:1.22 AS build
WORKDIR /src
COPY . .
RUN --mount=type=cache,target=/root/.cache/go-build \
go build -o /out/app ./cmd/appHere the binary goes to /out/app, which is a normal layer path. Writing the binary into the cache directory would lose it.
Do not rely on cache contents for correctness. A cache can be empty, pruned, or stale. The build must still produce the same result without it.
Pitfalls with secret mounts
Secrets are only available inside one RUN. The file appears at /run/secrets/<id> (or your target) while that instruction runs, then disappears. A later RUN cannot read it. Add the mount to every step that needs it.
A mismatched id fails quietly. If the Dockerfile says id=npmrc but you pass --secret id=npm, the file is simply missing. Some tools then fall back to anonymous access and fail with a confusing authentication error. Check that the ids match.
Do not print or copy the secret. The mount keeps the secret out of layers, but a command that copies it into the filesystem or echoes it to the build log undoes that protection. For example, cp /run/secrets/token /app/token writes the value into a layer.
CI logs can still expose values. Avoid set -x in steps that handle secrets, because the shell prints expanded commands. Supply secrets from your CI's secret store, using env= or src= sources.
Checking your work
After a build that uses secrets, inspect the image history:
docker history --no-trunc myappLook for tokens, .npmrc contents, or ARG values in the output. If you find any, a secret has leaked into metadata or a layer, and you should rotate it.
Quick reference
| Symptom | Likely cause | Fix |
|---|---|---|
| Every CI build downloads all packages | Fresh runner, empty cache mount | Persistent builder or restored cache directory |
| apt cache mount stays empty | `docker-clean` hook active | Remove hook, keep downloads |
| apt errors on parallel builds | Shared access to locked data | `sharing=locked` |
| Secret file not found | Mismatched id or missing mount on that `RUN` | Match ids, add mount per step |
| Binary missing after build | Output written inside the cache path | Write output outside the cache target |
Exercise
A CI job runs docker build --cache-from type=registry,ref=example/app:cache . and still downloads every pip package each time, even though the Dockerfile uses a type=cache mount for pip. Why, and what can you do?
Show answer
The registry cache stores layer results, not the contents of type=cache mounts. The runner is fresh, so the pip mount starts empty. Use a persistent builder, or restore the pip cache directory with a CI cache step. Layer cache will still help when requirements.txt has not changed.
Exercise
A build has RUN --mount=type=secret,id=token cat /run/secrets/token > /app/token.txt. Does this keep the secret out of the image?
Show answer
No. The mount itself is not stored, but the command writes the value into /app/token.txt, which becomes part of the layer. Use the secret only as input to a command, and never write it to the filesystem.