Sub topic 4
Pinning Tags and Digests for Reproducible Builds
This chapter assumes you can write a Dockerfile, build and push images, and know what a registry is. It covers how a base image reference can change under you, and how to pin it so your builds stay the same.
Why tags are not stable
A tag such as golang:1.22 is a movable label. The registry owner can point it at a new image at any time, for example when a patch release ships. Two builds of the same Dockerfile on different days can therefore pull different bases.
This causes two problems:
- Reproducibility. A build that worked yesterday can fail today, and you cannot rebuild an old release with the same inputs.
- Security. If a tag is overwritten by a compromised publisher, your next build pulls the changed image without any warning.
The tag latest is the worst case. It is only a default name, and it says nothing about version or stability.
Three levels of pinning
| Reference | Example | Moves? |
|---|---|---|
| No tag (means `latest`) | `node` | Yes, across major versions |
| Version tag | `node:20-slim` | Yes, with patch and OS updates |
| Digest | `node@sha256:...` | No |
A digest is a SHA-256 hash of the image manifest. Content determines the hash, so a digest always refers to the exact same bytes. If the content changes, the digest changes.
Finding and using a digest
Pull the image by tag, then read its digest:
docker pull golang:1.22
docker buildx imagetools inspect golang:1.22The first lines of the output include a Digest: value. You can also run docker images --digests. Use that value in your FROM line and keep the tag for readability:
# syntax=docker/dockerfile:1
FROM golang:1.22@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app ./cmd/appThe digest above is a placeholder. Replace it with the real value from your own inspect output, or the pull will fail. When a digest is present, Docker uses it and ignores the tag. The tag is then only a note for human readers.
Multi-architecture images have two kinds of digest. The digest of the index (manifest list) works on any platform and resolves to the right variant. The digest of a single platform manifest locks you to one architecture. Pin the index digest unless you build for one platform only.
The trade-off: pinned images do not update
Pinning stops surprise changes, but it also stops security patches. A digest-pinned base keeps its known vulnerabilities until you change it. Pinning is only safe when paired with a regular update routine.
Automate the routine:
- Use Dependabot or Renovate. Both can read Dockerfiles, find the newer digest for a tag, and open a pull request.
- Run your CI build and a vulnerability scan on each such pull request.
- Merge updates on a schedule, and sooner for critical fixes.
Common mistakes
- Pinning only the final stage in a multi-stage build. Every
FROMline should be pinned, including build stages. - Treating a version tag like
1.22as immutable. Only digests are. - Pinning a digest and never updating it.
- Copying a digest from a blog post instead of verifying it against the registry yourself.
Exercise
Your Dockerfile has FROM python:3.12-slim. A teammate says the build is reproducible because the tag includes the version. What is wrong with that claim, and what change fixes it?
Show answer
The tag 3.12-slim is mutable. The publisher moves it to new patch releases and OS updates, so two builds can use different bases. Pin the image by digest, for example python:3.12-slim@sha256:<digest>, and use a tool like Renovate or Dependabot to propose digest updates so the base still receives security patches.