Sub topic 4
Pinning Tags and Digests for Reproducible Builds
This chapter assumes you can write a Dockerfile, build and run images, and know what a registry is. You should also know that an image is made of layers and that a registry stores images by name and tag. The goal is to make sure the base image you build on today is the same one you build on next month, and to know when to change it on purpose.
Why a tag is not a version
A tag is a human-readable label that points at an image in a registry. Tags are mutable. The registry owner can push a new image under an existing tag at any time, and the old image stays in the registry but loses the label.
Take FROM node:20-slim. The 20-slim tag moves every time the maintainers publish a new Node 20 patch release or rebuild the image with updated Debian packages. Two builds of the same Dockerfile, a week apart, can produce different results. That causes three problems:
- Builds are not reproducible. A bug can appear in CI that you cannot reproduce locally, because the two machines pulled different images.
- Changes arrive unreviewed. A new base image can bring a changed library, a removed package, or a different default user. Nobody on your team approved it.
- A compromised tag affects you directly. If an attacker gains push access to a repository, or a maintainer account is hijacked, they can push a malicious image under a tag you already trust. Your next build pulls it.
Pinning means choosing a reference that always resolves to the same content. Docker gives you two levels of pinning, and they differ in strength.
Levels of pinning
| Reference | Example | Mutable? | Notes |
|---|---|---|---|
| No tag | `FROM node` | Yes | Means `latest`. Avoid it. |
| Floating tag | `FROM node:20-slim` | Yes | Receives patch updates and rebuilds. |
| Specific tag | `FROM node:20.11.1-slim` | Yes, in principle | Maintainers rarely move it, but they can, and many do rebuild it. |
| Digest | `FROM node@sha256:...` | No | Identifies exact content. |
| Tag plus digest | `FROM node:20.11.1-slim@sha256:...` | No | Readable and exact. |
A digest is a SHA-256 hash of the image manifest, the small JSON document that lists the image's layers and configuration. If any byte of any layer changes, the hash changes. A digest therefore names content, not a location or a label. A registry cannot serve different content for the same digest without the hash check failing.
When you give Docker both a tag and a digest, it uses only the digest to find the image. The tag is kept as a comment for human readers. This is why the combined form is the recommended style.
Finding and using a digest
You can read the digest of an image you have already pulled:
docker pull node:20.11.1-slim
docker image inspect node:20.11.1-slim --format '{{index .RepoDigests 0}}'The output looks like node@sha256: followed by 64 hexadecimal characters. You can also query the registry without pulling:
docker buildx imagetools inspect node:20.11.1-slimThis prints the digest of the top-level manifest, along with the platform-specific manifests it contains. Then put the digest in the Dockerfile. This example uses a placeholder, so substitute the real value from your own output:
# syntax=docker/dockerfile:1
FROM node:20.11.1-slim@sha256:<digest-from-your-own-output> AS base
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
USER node
CMD ["node", "server.js"]If the registry returns content that does not match the digest, the build fails. That failure is the behavior you want.
Multi-platform images and which digest to pin
Many official images are published as an image index (also called a manifest list). An index is a manifest that points to one image per platform, such as linux/amd64 and linux/arm64. The digest you copy from docker buildx imagetools inspect is normally the index digest. Pinning the index digest keeps builds reproducible on every platform: each machine resolves the index and picks the matching platform image, and the result is fixed for each platform.
Be careful with docker image inspect. On a machine that pulled the image, RepoDigests usually shows the index digest, but check it against the registry output when you work across architectures. If you accidentally pin a single-platform manifest digest copied from an amd64 machine, builds on an arm64 machine will fail with a platform mismatch.
Pinning is not the same as being secure
Pinning freezes an image, including any vulnerabilities in it. A digest you pinned six months ago still contains the packages from six months ago. If you pin and never update, you trade unreviewed change for unpatched flaws.
The goal is controlled updates. You decide when the base image changes, you see what changed, and you can roll back. This works only if updating is cheap, so pinning needs a process to go with it.
Automating digest updates
Editing digests by hand is error-prone, so most teams let a tool open pull requests. Dependabot and Renovate both understand Dockerfiles. They detect FROM lines, look up the newest tag and digest, and propose a change that your CI can test before it is merged.
A minimal Dependabot configuration, stored at .github/dependabot.yml, looks like this:
version: 2
updates:
- package-ecosystem: "docker"
directory: "/"
schedule:
interval: "weekly"Each proposed change shows the old and new digest in a diff. You review it, let the tests and the scanner run, and merge. Your history then records every base image change, which also helps during incident response.
Pinning other things in the build
The base image is the biggest source of drift, but it is not the only one.
- Other images. Pin every
FROMline, including build stages, and any image used inCOPY --from=ordocker runcommands in scripts and Compose files. A single unpinned stage undermines the rest. - Language dependencies. Commit lock files (
package-lock.json,go.sum,poetry.lock) and install with commands that respect them, such asnpm ciinstead ofnpm install. - OS packages.
apt-get install curlinstalls whatever version the mirror serves today. You can pin versions withcurl=7.88.1-10+deb12u5, but those versions disappear from mirrors as they are superseded, which breaks the build. Many teams accept floating OS package versions inside a pinned base image and rely on scanning instead. Choose deliberately and write down the decision. - Downloaded files. If you
curla binary in aRUNstep, verify its checksum withsha256sum -c.
Edge cases and common errors
Digest removed from the registry. Registries can delete old images, and some apply retention policies. If your pinned digest is gone, the build fails with a "not found" error. For images your business depends on, mirror them into a registry you control and pin the digest there.
Pinned digest differs between registries. The same image pushed to two registries can have different digests if it was re-pushed in a different format. Pin against the registry you actually pull from.
Local cache hides the problem. If an image is already on your machine, docker build may use it without checking the registry for a newer one. To force a fresh check for a floating tag, use docker build --pull. With a digest this is unnecessary, because the content cannot change.
Pinning only the final stage. In a multi-stage build, the build stage often has the most dependencies. Pin both.
Using latest in production. Even if latest is "stable" today, it is the least predictable reference you can use. It also makes rollbacks difficult, because you cannot tell which image latest meant last Tuesday.
Pinning your own images. The same rule applies to images you build. Deploy by digest or by an immutable version tag, never by latest. Many registries support tag immutability, which refuses pushes to an existing tag. Turn it on for release tags.
A practical policy
- Choose a specific version tag, such as
20.11.1-slim, instead of a floating one. - Add the digest next to it in every
FROMline. - Commit lock files for application dependencies.
- Enable an update bot so changes arrive as reviewed pull requests.
- Scan every candidate image before merging.
- Mirror critical base images into a registry you control.
- Deploy your own images by digest.
Exercise
A Dockerfile says FROM python:3.12-slim@sha256:<digest>. Upstream maintainers publish a new image under the tag python:3.12-slim with a fixed OpenSSL vulnerability. What will your next docker build use, and what must you do to get the fix?
Show answer
The build still uses the old image, because Docker resolves the digest and ignores the tag when both are present. To get the fix, update the digest in the Dockerfile, ideally through a pull request from an update tool, then let CI build and scan the new image before merging.
Exercise
You copied a digest from docker image inspect on an amd64 laptop, and the build now fails on an arm64 CI runner. What is the likely cause, and how do you fix it?
Show answer
You pinned the digest of a single-platform manifest instead of the image index. Run docker buildx imagetools inspect <image> and pin the top-level index digest, which lists a manifest for each platform.
Exercise
Why is pinning a digest alone not enough to keep a container secure over time?
Show answer
A digest freezes the image contents, including known vulnerabilities. Without a process that proposes updates and scans new images, the pinned image goes stale and stops receiving security patches.