Sub topic 4
Giving one image several tags
This chapter assumes you can build and push images, know that a registry stores images under a repository name, and understand MAJOR.MINOR.PATCH versions. Here you will learn how one image can carry many tags and which commands add them.
One image, many labels
A tag is a label that points at image content. The content is identified by its digest, a hash of the image manifest. Several tags can point at the same digest, and adding a tag never copies or rebuilds anything.
Because the tags share one digest, they are guaranteed to run identical code. That is the main reason to add tags to an existing image instead of rebuilding it for each name.
Tagging at build time
The -t flag of docker build names the result. You can pass it more than once.
docker build -t registry.example.com/team/myapp:1.4.2 \
-t registry.example.com/team/myapp:latest .Both tags are attached to the same build output. This is fine for two names, but it gets long as the list grows.
Adding tags with docker tag
The docker tag SOURCE TARGET command creates a new reference to an image that already exists locally. The source can be any tag or an image ID.
IMAGE=registry.example.com/team/myapp
docker build -t $IMAGE:1.4.2 .
docker tag $IMAGE:1.4.2 $IMAGE:1.4
docker tag $IMAGE:1.4.2 $IMAGE:1
docker tag $IMAGE:1.4.2 $IMAGE:latest
docker push --all-tags $IMAGEThe build creates 1.4.2. Each docker tag call adds one more name for the same content. The last line pushes every local tag of that repository.
You can check that the tags share an image ID:
docker images registry.example.com/team/myappThe IMAGE ID column shows the same value on each row.
What docker push sends
Pushing several tags does not upload the layers several times. The registry already has the layers after the first tag, so later tags only add a small manifest entry that points to them. Pushing four tags costs little more than pushing one.
docker push --all-tags pushes every local tag in the repository, including old tags left over from earlier builds. Check docker images first on a machine that has built many versions. Otherwise you may move a tag you did not intend to move. To push a single tag, name it:
docker push registry.example.com/team/myapp:1.4Moving a tag
Running docker tag with a target that already exists silently moves that tag to the new image. Locally, the old image keeps its ID but loses the name. After you push, the registry points the tag at the new content as well.
docker tag $IMAGE:1.4.3 $IMAGE:1.4
docker push $IMAGE:1.4This is how rolling tags are advanced, and it is also how mistakes happen. The next chapters build on this behavior.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `No such image: myapp:1.4.2` | The source tag does not exist locally. | Build or pull it first, then run `docker tag`. |
| `invalid reference format` | The tag has spaces, uppercase repository letters, or a leading `-` or `.`. | Use a valid name. Repository names must be lowercase. |
| A tag shows an old image on another machine | You tagged locally but did not push that tag. | Run `docker push` for that tag. |
| Unexpected tags appear in the registry | `--all-tags` pushed leftover local tags. | Remove stale tags with `docker rmi` or push tags by name. |
Exercise
You built myapp:1.4.2 locally. Which commands give it the extra tags 1.4 and latest, and do those commands change the image ID?
Show answer
Run docker tag myapp:1.4.2 myapp:1.4 and docker tag myapp:1.4.2 myapp:latest. The image ID does not change. Both commands only add names that point at the existing image content.
Exercise
A teammate runs docker tag myapp:1.5.0 myapp:1.4 and pushes 1.4. What happens to anyone who deploys myapp:1.4?
Show answer
The 1.4 tag now points at the 1.5.0 content. Deployments that pull 1.4 get the new image, even though the tag name suggests a 1.4 release.