Registries & Tags
A registry stores images so other machines can pull them. Docker Hub (docker.io) is the default; others include GitHub Container Registry (ghcr.io), GitLab's registry, Amazon ECR, Google Artifact Registry, Azure Container Registry, and self-hosted ones such as Harbor or the open-source registry:2 image.
Inside a registry, images are grouped into repositories (library/nginx, myteam/shop-api), and each repository holds many tags (1.27, 1.27-alpine, latest).
Anatomy of an image reference
ghcr.io/acme/shop-api:2.4.1@sha256:9f86d081884c...
\_____/ \___________/ \___/ \__________________/
registry repository tag digestMost parts are optional, and Docker fills in defaults: no registry means docker.io, a single-word repository on Docker Hub means the library/ namespace of official images, and no tag means latest. So nginx is really docker.io/library/nginx:latest.
def parse_reference(ref):
digest = None
if "@" in ref:
ref, digest = ref.split("@", 1)
first, _, rest = ref.partition("/")
# the first part is a registry only if it looks like a host name
if rest and ("." in first or ":" in first or first == "localhost"):
registry, path = first, rest
else:
registry, path = "docker.io", ref
tag = "latest"
last = path.rsplit("/", 1)[-1]
if ":" in last:
path, tag = path.rsplit(":", 1)
if registry == "docker.io" and "/" not in path:
path = "library/" + path
return registry, path, tag, digest
for ref in ["nginx",
"python:3.12-slim",
"bitnami/redis:7.4",
"ghcr.io/acme/shop-api:2.4.1",
"localhost:5000/tools/linter",
"alpine@sha256:4bcff63911fc"]:
registry, repo, tag, digest = parse_reference(ref)
print(f"{ref:<28} -> {registry}/{repo}:{tag}" + (f" @{digest}" if digest else ""))nginx -> docker.io/library/nginx:latest
python:3.12-slim -> docker.io/library/python:3.12-slim
bitnami/redis:7.4 -> docker.io/bitnami/redis:7.4
ghcr.io/acme/shop-api:2.4.1 -> ghcr.io/acme/shop-api:2.4.1
localhost:5000/tools/linter -> localhost:5000/tools/linter:latest
alpine@sha256:4bcff63911fc -> docker.io/library/alpine:latest @sha256:4bcff63911fc
Note the tricky case localhost:5000/tools/linter: the colon belongs to the registry's port, not to a tag. The parser only looks for a tag after the last slash.
Pulling, tagging and pushing
docker pull nginx:1.27-alpine
docker tag myapp:latest ghcr.io/acme/myapp:1.0.0
docker login ghcr.io
docker push ghcr.io/acme/myapp:1.0.0Sample output of the push:
The push refers to repository [ghcr.io/acme/myapp]
5f70bf18a086: Pushed
a8c1e3d2b4f6: Mounted from acme/base
d4fc045c9e3a: Layer already exists
1.0.0: digest: sha256:7c3e0f1a2b... size: 1365docker tag does not copy anything: it adds another name for the same image ID. The push then uploads only the layers the registry does not already have ("Layer already exists").
Tags are mutable, digests are not
A tag is a pointer that the publisher can move. python:3.12 pointed to 3.12.0 when it was released and points to the newest 3.12.x today. latest is just a tag name with no magic: it is whatever was last pushed under that name (or tagged latest on purpose).
registry = {} # tag -> digest
def push(tag, digest):
old = registry.get(tag)
registry[tag] = digest
print(f"push {tag:<12} -> {digest}" + (f" (moved from {old})" if old else ""))
push("3.12.0", "sha256:aaa1")
push("3.12", "sha256:aaa1")
push("latest", "sha256:aaa1")
push("3.12.1", "sha256:bbb2")
push("3.12", "sha256:bbb2")
push("latest", "sha256:bbb2")
print()
print("pinned by version tag 3.12.0 ->", registry["3.12.0"])
print("floating tag 3.12 ->", registry["3.12"])push 3.12.0 -> sha256:aaa1
push 3.12 -> sha256:aaa1
push latest -> sha256:aaa1
push 3.12.1 -> sha256:bbb2
push 3.12 -> sha256:bbb2 (moved from sha256:aaa1)
push latest -> sha256:bbb2 (moved from sha256:aaa1)
pinned by version tag 3.12.0 -> sha256:aaa1
floating tag 3.12 -> sha256:bbb2
A tagging strategy
Good images get several tags at release time so users can choose how much change they accept:
| Tag | Moves when | Use it for |
|---|---|---|
2.4.1 |
never (by convention) | production deployments |
2.4 |
each patch release | auto-pick bug fixes |
2 |
each minor release | relaxed environments |
latest |
every release | quick local experiments only |
sha-3f1c9a0 |
never | tracing an image back to a Git commit |
This model computes the tag set for a semantic version, the way many CI pipelines do:
def release_tags(version, commit, is_newest=True):
major, minor, patch = version.split(".")
tags = [version, f"{major}.{minor}", major, f"sha-{commit[:7]}"]
if is_newest:
tags.append("latest")
return tags
print(release_tags("2.4.1", "3f1c9a0d7b2e"))
print(release_tags("1.9.7", "77ab01c4e5f6", is_newest=False))['2.4.1', '2.4', '2', 'sha-3f1c9a0', 'latest']
['1.9.7', '1.9', '1', 'sha-77ab01c']
Warning: never deploy
:latestto production. You can't tell which version is running, and a restart may silently pull a different image.
Private registries and authentication
docker login <registry> stores credentials (ideally in a credential helper such as the OS keychain, not in plain text in ~/.docker/config.json). In CI, log in with a short-lived token rather than your password. Docker Hub limits anonymous pulls, so authenticated pulls or a mirror are worth setting up for busy build servers.
Test yourself
What full reference does docker pull redis use?
- redis.io/redis:stable
- docker.io/library/redis:latest
- docker.io/redis/redis:latest
- localhost/redis:latest
In localhost:5000/tools/linter, what is the tag?
- 5000
- tools
- linter
- latest (the default)
Which reference is guaranteed to always mean the same image content?
- myapp:latest
- myapp:2.4
- myapp@sha256:9f86d0...
- myapp:stable