Sub topic 3
Choosing and pinning base image tags
This chapter assumes you can write a basic Dockerfile and run docker build. It covers how to pick the image named in FROM and how to make that choice repeatable.
What a tag actually is
A tag is a movable label on an image in a registry. The reference python:3.12-slim means "the image currently labeled 3.12-slim in the python repository." The maintainers can move that label to a new image at any time, for example when they publish a security patch. Two builds of the same Dockerfile on different days can therefore start from different bases.
A digest is different. It is a SHA-256 hash of the image content, and it never moves.
Choosing a variant
Official images usually come in several flavors. The tag suffix tells you which one.
| Tag style | Example | Trade-off |
|---|---|---|
| Full | `python:3.12` | Large. Includes compilers and common tools. |
| Slim | `python:3.12-slim` | Smaller. Fewer packages, so some builds need extra installs. |
| Alpine | `python:3.12-alpine` | Very small. Uses musl instead of glibc, which can break prebuilt binary packages. |
| Scratch | `scratch` | Empty. Only works for static binaries. |
A slim image is a reasonable default for most applications. Pick Alpine only after checking that your dependencies work with musl.
Pinning levels
You can pin a base image at several levels of precision.
python:latestorpython: no version at all. The result changes without warning.python:3: the major version only. Minor releases arrive automatically.python:3.12-slim: the minor version. Patch updates still arrive.python:3.12.4-slim: a specific patch release. The label can still be re-pushed with rebuilt contents.python:3.12.4-slim@sha256:<digest>: an exact image. Nothing can change.
# Readable tag plus an immutable digest
FROM python:3.12.4-slim@sha256:<digest>
WORKDIR /appReplace <digest> with the real value before building. You can read it after pulling the image:
docker pull python:3.12.4-slim
docker image inspect --format '{{index .RepoDigests 0}}' python:3.12.4-slimWhen a digest is present, Docker uses it and ignores the tag for resolution. The tag stays in the file so a human can tell what the image is.
flowchart TD
A[FROM reference] --> B{Digest present?}
B -- yes --> C[Pull exact image by digest]
B -- no --> D[Look up tag in registry]
D --> E[Image the tag points to today]Why this matters for caching
The build cache for FROM depends on the resolved image. If a tag moves, the new base has a new ID, and every later layer is rebuilt. Pinning gives you stable cache hits. It also means you do not get security fixes unless you update the pin yourself.
Practical guidance
- Avoid
latestin any Dockerfile you commit. It hides which version you run. - Use the same base tag in development and production so that "works on my machine" differences stay small.
- Match the base to the toolchain. For a Go build stage, use
golang:1.22. For the final stage, you can use a smaller base. - Prefer official or verified publisher images. Check who maintains an image before you trust it.
Exercise
Your Dockerfile starts with FROM node:20. A teammate reports that a build last week passed and today's build fails, with no source changes. Give one likely cause and one change to the FROM line that prevents it.
Show answer
The node:20 tag is movable, so it now points to a newer minor or patch release (or a rebuilt image) than last week. Pin a specific version, such as node:20.14.0-slim, or add a digest with @sha256:<digest>, so every build starts from the same base.
Wait. I need to fix the diagram and callout fences, since they must use the required info strings.