Sub topic 2
Reading an image reference: host, repository, tag, digest
This chapter assumes you can build an image and run a container with Docker, and that you know what a registry is from the previous chapter. Every pull, push, and deployment manifest depends on the image reference, so you need to read it correctly.
The four parts
An image reference has up to four parts:
ghcr.io/your-github-username/my-app:1.0.0@sha256:3b7e...
|_____| |___________________________| |___| |__________|
host repository tag digest- Host is the registry's DNS name, optionally with a port, such as
ghcr.ioor123456789012.dkr.ecr.us-east-1.amazonaws.com. - Repository is the path that names one image. It is often
owner/name, but registries differ in how many path segments they allow. - Tag is a human-readable label after
:. It is a movable pointer to a manifest. - Digest is the SHA-256 hash of the manifest, written after
@. It identifies exactly one image.
Repository names must be lowercase. This is why GHCR pushes fail when your GitHub username contains capital letters.
Defaults Docker fills in
Docker shortens references when parts are missing:
| You write | Docker reads it as |
|---|---|
| `nginx` | `docker.io/library/nginx:latest` |
| `myorg/app` | `docker.io/myorg/app:latest` |
| `ghcr.io/myorg/app` | `ghcr.io/myorg/app:latest` |
A first path segment is treated as a host only if it contains a . or a :, or is localhost. That is why ghcr.io/... goes to GHCR, while myorg/app goes to Docker Hub. Leaving off the tag means latest, which is only a convention and does not mean "newest".
Tags move, digests do not
A tag is a pointer. Anyone with push rights can point 1.0.0 at a different image later, and the next pull gets the new content. A digest is computed from the manifest content, so a reference containing a digest always resolves to the same bytes.
When a reference has both tag and digest, Docker and Kubernetes pull by the digest and ignore the tag. The tag then serves only as a note for readers.
Finding and using a digest
Push an image, then read the digest the registry assigned:
docker push ghcr.io/your-github-username/my-app:1.0.0
docker buildx imagetools inspect ghcr.io/your-github-username/my-app:1.0.0The push output ends with a line such as 1.0.0: digest: sha256:... size: 1570. To pull by digest, replace the tag:
docker pull ghcr.io/your-github-username/my-app@sha256:3b7e...Use the full 64-character hash. Abbreviated digests are not accepted in references.
Common mistakes
- Using
latestin a deployment. Two nodes can pull at different times and run different code. - Assuming a tag is stable. Unless the registry enforces tag immutability, a tag can be overwritten.
- Uppercase letters in the repository path.
- Copying a digest from a multi-architecture image and expecting it to match a single platform. The digest of the index differs from the digest of each platform's manifest.
Exercise
Docker is given acme/api. Which registry host, repository, and tag does it use, and why?
Show answer
Host docker.io, repository acme/api, tag latest. The first segment acme has no . or : and is not localhost, so Docker does not treat it as a host and defaults to Docker Hub. The missing tag defaults to latest.
Exercise
A reference reads ghcr.io/acme/api:2.1.0@sha256:aaaa.... Someone later pushes a different image to the tag 2.1.0. Which image does a pull of this reference return?
Show answer
The image with the digest sha256:aaaa.... When both tag and digest are present, the digest decides, so the retagged image is ignored.