Sub topic 2
Why tags can move
This chapter assumes you can build, run, and push Docker images, that you know the registry/repository:tag structure of an image reference, and that you understand Git commits and MAJOR.MINOR.PATCH versions. Here you will learn what a tag is, how it can change meaning over time, and what that means for deployments.
A tag is a pointer, not a snapshot
An image is stored as content. A registry identifies that content by a digest, which is a SHA-256 hash of the image manifest. A tag is a short, human-readable name that the registry maps to one digest.
A tag does not hold the image. It points at one. The registry stores the mapping, and whoever has push rights can change it.
docker pull registry.example.com/team/myapp:1.4This command asks the registry "which digest does 1.4 point to right now?" and then downloads that content. Run it next month and the answer may differ.
How a tag moves
Pushing an image with a tag that already exists does not fail. The registry moves the tag to the new digest. The old content stays in the registry, but it no longer has that name. If no other tag or digest reference points to it, it is called untagged (or dangling) and may later be removed by garbage collection.
The diagram below shows the sequence.
Both pulls used the same reference string. They returned different images.
Seeing it locally
You can observe the same behavior on your own machine. Retagging a local image changes which image the name refers to.
docker build -t myapp:demo .
docker images --digests myapp
# change the code, then build again with the same tag
docker build -t myapp:demo .
docker images --digests myappAfter the second build, myapp:demo refers to the new image. The previous image is listed with <none> as its tag, because the name moved away from it.
What this means for deployments
Mutable tags cause problems in a few predictable places:
- Reproducibility. Two servers that pull
myapp:1.4at different times can run different code. - Rollbacks. If you overwrote
1.4with a bad build, there is no tag for the good build anymore. You need its digest or another tag that still points to it. - Auditing. Logs that record only
myapp:1.4do not tell you which content ran. - Caching. A node that already has
myapp:1.4may not re-pull it, depending on the pull policy. Kubernetes, for example, defaults toIfNotPresentfor tags other thanlatest. A moved tag can therefore leave a cluster running mixed versions.
Mutable by design, immutable by convention
Docker itself does not enforce that a tag stays put. Mutability is a feature. Tags like latest and 1.4 are useful because they move: they let you say "the newest build" or "the newest 1.4.x patch" without editing every consumer.
The practical rule is to choose, per tag, whether it is allowed to move:
| Tag kind | Example | Should it move? |
|---|---|---|
| Full version | `1.4.2` | No. Treat as immutable. |
| Commit SHA | `sha-3f9a1c2` | No. One commit, one image. |
| Rolling minor or major | `1.4`, `1` | Yes, to the latest matching release. |
| Latest | `latest` | Yes, on every release. |
Later chapters build tagging schemes around this split. Registries can also enforce immutability for you, which is covered in the chapter on registry policies.
Checking what a tag points to now
To see the digest behind a tag you have pulled, inspect it.
docker pull registry.example.com/team/myapp:1.4
docker inspect --format '{{index .RepoDigests 0}}' registry.example.com/team/myapp:1.4The output has the form registry.example.com/team/myapp@sha256:.... Unlike a tag, a digest always refers to exactly the same content.
Exercise
You push myapp:2.0 on Monday. On Wednesday a teammate fixes a bug and pushes a new build with the same tag myapp:2.0. Staging pulled on Tuesday, and production pulls on Thursday. What do the two environments run, and why?
Show answer
Staging runs the Monday image and production runs the Wednesday image. The tag 2.0 is a pointer. The Wednesday push moved it to a new digest, so each environment got whichever digest the tag pointed at when it pulled. Using a new tag such as 2.0.1, or pinning by digest, would avoid the mismatch.