Sub topic 12
Pinning by digest and keeping base images current
This chapter assumes you can write a Dockerfile, run docker build, and read the scanning commands from the previous chapter. It covers how to make base image choices reproducible and how to keep them from going stale.
Tags move, digests do not
A tag such as python:3.12-slim is a label that the image publisher can point at a new image at any time. Publishers do this on purpose. They rebuild the tag to pick up security patches in Debian packages or a new Python patch release. Two builds of the same Dockerfile on different days can therefore produce different images.
A digest is a SHA-256 hash of the image manifest. It identifies one exact image, and it cannot be reassigned. If you reference an image by digest, you get the same bytes every time, or the pull fails.
| Reference | Example | Changes over time? |
|---|---|---|
| Floating tag | `python:3` | Yes, including minor and major versions |
| Specific tag | `python:3.12-slim` | Yes, rebuilt for patches |
| Digest | `python@sha256:...` | No |
| Tag plus digest | `python:3.12-slim@sha256:...` | No. The digest wins and the tag is documentation |
Finding and using a digest
Inspect the image in the registry, or pull it and read the local metadata:
docker buildx imagetools inspect python:3.12-slim
docker pull python:3.12-slim
docker inspect --format '{{index .RepoDigests 0}}' python:3.12-slimThe digest of a multi-architecture tag identifies the image index, which lists one image per platform. Pinning the index digest still lets an arm64 laptop and an amd64 server each pull the right image.
Put the digest after the tag in FROM. This works the same for Debian slim, Alpine, distroless, and Docker Hardened Images:
# Replace the placeholders with an image and tag from the Docker Hardened Images catalog
FROM <registry>/<hardened-image>:<tag>@sha256:<digest>
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]Keep the tag next to the digest. Docker ignores the tag when a digest is present, but the tag tells a human reader which version the digest was taken from.
The cost of pinning
A pinned digest never changes, so it also never receives security fixes. If you pin once and forget it, the base image slowly accumulates known vulnerabilities. Pinning trades surprise changes for stale ones, and you have to handle the staleness yourself.
Automating updates
Update tools read your Dockerfiles, compare the pinned digest with the registry, and open a pull request when it changes. Dependabot and Renovate both do this. A minimal Dependabot configuration looks like this:
version: 2
updates:
- package-ecosystem: "docker"
directory: "/"
schedule:
interval: "weekly"Each pull request then runs your normal CI. Add a scan step so that a digest bump is merged only if the new image is no worse than the old one:
docker build --pull -t myapp:candidate .
docker scout cves myapp:candidate
trivy image myapp:candidate--pull makes the builder check the registry for the base image instead of reusing a local copy.
Rebuilding without changing the Dockerfile
Your own code may not change for weeks, but the base image does. Rebuild and redeploy on a schedule, for example weekly, even when nothing in the repository changed. Without this, a running service keeps the OS packages from its last build.
Two settings affect this:
- Layer cache. Cached layers hide new base content. Use
--pullin CI, and--no-cachefor scheduled rebuilds when you need a clean result. - Package installs in your own layers. A
RUN apk addorapt-get installis only re-run when its layer is rebuilt. Pinning the base does not freeze these packages, but a cached layer will.
Practical guidance
- Pin production images by digest. Floating tags are acceptable for local development.
- Pin both the build stage and the runtime stage in a multi-stage build. The runtime stage is what ships, but an unpinned build stage can change your compiled output.
- Prefer a specific tag such as
3.12-slimoverlatest, so that automated bumps stay within a version you chose. - Hardened image catalogs rebuild frequently to ship fixes, so update automation matters most there.
Exercise
Your Dockerfile has FROM python:3.12-slim@sha256:<digest>. The publisher releases a Debian security patch and rebuilds the 3.12-slim tag. What happens to your next build, and what should you set up?
Show answer
Your build still pulls the old image, because the digest identifies the old content and the tag is ignored. You do not get the patch. Set up an update bot such as Dependabot or Renovate to open pull requests with the new digest, run a build and scan in CI, and also rebuild on a schedule using docker build --pull so the fix reaches production.