Sub topic 1
Anatomy of an image reference
Every Docker command that names an image uses an image reference. The tagging schemes in later chapters all work by changing parts of this reference, so you need to be able to read one precisely.
Prerequisites
This chapter assumes you can:
- Build, run, and push an image with Docker.
- Read basic Git output, such as commit hashes.
- Describe what a container registry does.
- Read a version number in MAJOR.MINOR.PATCH form.
The four parts
A full reference has up to four parts:
registry.example.com:5000/team/myapp:1.4.2@sha256:3b4c5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f708192a
\_____________________/ \__________/ \___/ \____________________________________________________________________/
registry repository tag digest- Registry is the server that stores images. It is a hostname with an optional port, such as
registry.example.com:5000. - Repository is the named collection of related images on that registry. It is a path, such as
team/myapp. - Tag is a human-readable label after a colon, such as
1.4.2. - Digest is a content hash after an
@, in the formsha256:followed by 64 hexadecimal characters. It identifies the exact image content.
Only the repository is required. Docker fills in the other parts when you leave them out.
Defaults Docker applies
| You write | Docker reads it as |
|---|---|
| `nginx` | `docker.io/library/nginx:latest` |
| `nginx:1.27` | `docker.io/library/nginx:1.27` |
| `team/myapp` | `docker.io/team/myapp:latest` |
| `registry.example.com/team/myapp` | `registry.example.com/team/myapp:latest` |
Two rules explain the table:
- If the first path component contains a
.or a:, or islocalhost, Docker treats it as a registry host. Otherwise the registry is Docker Hub (docker.io). - If you give no tag and no digest, Docker uses the tag
latest. This is only a default name, and thelatestchapter covers it in detail.
On Docker Hub, single-name images such as nginx live under the library/ namespace.
Tag versus digest
A tag and a digest both point at an image, but they behave differently. A tag is a name that the registry maps to an image, and the owner of the repository can later point that name at different content. A digest is computed from the image manifest, so the same digest always means the same content.
You can see both for an image you have pulled:
docker pull registry.example.com/team/myapp:1.4.2
# The tag as you know it
docker image ls registry.example.com/team/myapp
# The digest the registry reported for that tag
docker inspect --format '{{index .RepoDigests 0}}' registry.example.com/team/myapp:1.4.2You can pull by digest instead of by tag:
docker pull registry.example.com/team/myapp@sha256:3b4c5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f708192aReading references in the wild
Misreading a reference is a common source of errors. Watch for these cases:
- A port is not a tag. In
localhost:5000/myapp, the:5000belongs to the registry host. The tag is the colon after the last/, so this reference has no tag and defaults tolatest. - Repository names must be lowercase.
Team/MyAppis rejected, while the tag may contain uppercase letters. - The same image content can have many references.
myapp:1.4.2,myapp:1.4, andmyapp:latestcan all resolve to one digest.
Exercise
What registry, repository, and tag does Docker use for the reference localhost:5000/team/api, and why is :5000 not the tag?
Show answer
The registry is localhost:5000, the repository is team/api, and the tag defaults to latest. The :5000 comes before the first /, so it is the registry's port. A tag is only the part after a colon in the final path component.