Sub topic 12
Choosing tags: versions, immutability, latest, and digests
This chapter assumes you can build an image with docker build -t, and that you know how to retag and push an image to a registry. Here you decide which tags to publish and how to refer to an image so that deployments stay predictable.
A tag is a movable label
A tag is a pointer to an image. Nothing stops you from pushing a different image under a tag that already exists. The registry moves the pointer, and anyone who pulls that tag afterward gets the new image.
The only fixed identifier is the digest. A digest is a SHA-256 hash of the image manifest, and it looks like sha256:3b4c.... Changing any byte of the image changes the digest.
Version tags
Use semantic versions for releases. You can publish several tags that point to the same image, each with a different level of precision.
| Tag | Moves when | Typical use |
|---|---|---|
| `1.4.2` | Never, if you treat it as immutable | Production deployments |
| `1.4` | A new patch release ships | Receiving patch fixes automatically |
| `1` | A new minor release ships | Following a major version |
| `latest` | Any release ships | Quick local experiments |
Build once and apply all the tags to the same image:
docker build -t myapp:1.4.2 -t myapp:1.4 -t myapp:1 -t myapp:latest .For traceability, you can also tag with the short Git commit hash. A tag such as myapp:git-9fceb02 maps an image back to its source.
docker build -t myapp:git-$(git rev-parse --short HEAD) .Immutability
Treat release tags as write-once. If 1.4.2 has a bug, publish 1.4.3. Do not push a fixed image over 1.4.2.
Overwriting a release tag causes two problems. Machines that already pulled 1.4.2 keep the old image, while new machines get the new one. Two environments then run different code under the same name.
Some registries can enforce immutability. Amazon ECR, for example, has a per-repository setting that rejects a push to an existing tag. Where you cannot enforce it, make it a team rule.
The latest tag
latest is an ordinary tag with one special behavior. Docker uses it when you give an image name without a tag. Both of these refer to myapp:latest:
docker build -t myapp .
docker run myapplatest does not mean "most recent" or "newest". It is only the tag that was applied last, if someone applied it. Avoid latest in production manifests, because you cannot tell which build is running. Use it for local work, or as a convenience alias that you move deliberately on each release.
Pinning by digest
A digest reference always resolves to exactly one image. Find the digest after a push, or read it from a local image:
docker push registry.example.com/team/myapp:1.4.2
docker image inspect --format '{{index .RepoDigests 0}}' registry.example.com/team/myapp:1.4.2The output has the form registry.example.com/team/myapp@sha256:.... Pull or run it with the @ syntax:
docker pull registry.example.com/team/myapp@sha256:3b4c0e1f...You can also pin base images in a Dockerfile. Keep the tag for readability, and add the digest for exactness:
FROM python:3.12-slim@sha256:3b4c0e1f...The cost is that you no longer receive base image security updates automatically. You must update the digest yourself, ideally with a dependency update tool.
A practical strategy
- Tag every build with the commit hash.
- Tag releases with the full version, such as
1.4.2, and never overwrite it. - Move
1.4,1, andlatestonly on release. - Deploy production from the full version tag, or from the digest when you need a guarantee.
Exercise
Your production manifest uses myapp:latest. After a release, one server runs old code and another runs new code. Why can this happen, and what two changes prevent it?
Show answer
Each server pulled latest at a different time, and the tag moved between pulls. Deploy from an immutable version tag such as myapp:1.4.2, or from the digest (myapp@sha256:...), so every server resolves to the same image.