Docker Best Practices

Chapter 2 · Practice 2 of 5

Pin base images by digest for reproducible builds

From Image size and security

Why it matters

Tags are mutable: the publisher can move node:24-slim to a new image at any time, so two builds of the same commit can differ. Pinning to a digest (@sha256:...) means every build uses the exact same base.

Tag onlyTag plus digest
Same commit, same imageNot alwaysYes
Picks up base updatesAutomaticallyThrough a pull request
Readable by humansYesYes, keep the tag next to it

The cost is that you must update digests yourself, so pair pinning with a bot. Docker's docs point to Dependabot's docker ecosystem, which opens pull requests when a new digest is published.

Example

Dockerfile
FROM node:24-slim@sha256:<digest-from-docker-buildx-imagetools-inspect>

# .github/dependabot.yml
# - package-ecosystem: "docker"
#   directory: "/"
#   schedule:
#     interval: "weekly"