Sub topic 2
Reading and writing image names
This chapter assumes you have Docker installed and running, can use a terminal, and have built an image from a Dockerfile. You don't need to be logged in to Docker Hub yet. Every later step in this tutorial (tagging, pushing, pulling) depends on the image name being correct. Many "denied" and "not found" errors are naming mistakes.
The anatomy of an image reference
An image reference is the string you pass to commands such as docker pull, docker tag, and docker push. A full reference has up to four parts:
registry-host/namespace/repository:tag| Part | Example | Meaning |
|---|---|---|
| Registry host | `docker.io` | The server that stores the images. |
| Namespace | `alice` | The account or organization that owns the repository. |
| Repository | `myapp` | A named collection of related image versions. |
| Tag | `1.0` | A label that points to one version in the repository. |
A fifth part, the digest, can replace the tag. It is covered below.
Docker Hub combines the namespace and repository into one path, such as alice/myapp. On Docker Hub, the namespace is your username or the name of an organization you belong to. The repository is what you create (or let Docker Hub create on first push) under that namespace.
What Docker fills in for you
You rarely type the full form. The Docker CLI applies three defaults when a part is missing.
- No registry host: Docker uses
docker.io, which is Docker Hub. - No namespace (a single-word name): Docker uses
library, the namespace for Docker Official Images. - No tag: Docker uses
latest.
These references are therefore identical:
docker pull alpine
docker pull alpine:latest
docker pull docker.io/library/alpine:latestAll three fetch the same image. The short name alpine is a convenience for the long form.
The defaults also explain a common mistake. If you run docker push myapp:1.0 with an image that has no namespace, Docker reads it as docker.io/library/myapp:1.0. You can't write to library, so the registry refuses the push. The name tells Docker where to send the image, so the name must include your namespace before you can push to your own repository.
How Docker decides whether the first part is a registry host
When a reference contains a slash, Docker has to decide whether the text before the first slash is a registry host or a namespace. The rule is simple. The first component is treated as a registry host if it contains a dot (.) or a colon (:), or if it is exactly localhost. Otherwise it is a namespace on Docker Hub.
| Reference | First component | Interpreted as |
|---|---|---|
| `alice/myapp` | `alice` | Namespace `alice` on Docker Hub. |
| `ghcr.io/alice/myapp` | `ghcr.io` | Registry host (contains a dot). |
| `localhost:5000/myapp` | `localhost:5000` | Registry host (a local registry). |
| `registry.internal/team/api` | `registry.internal` | Registry host (contains a dot). |
| `myregistry/api` | `myregistry` | Namespace on Docker Hub, not a host. |
The last row catches people out. A private registry whose hostname has no dot and no port, such as myregistry, is read as a Docker Hub namespace. Add a port (myregistry:5000/api) or use a fully qualified hostname.
Tags
A tag is a human-readable label for one version of an image. Tags live inside a repository, so alice/myapp:1.0 and alice/other:1.0 are unrelated.
Tag syntax rules:
- Allowed characters are letters, digits, underscores, periods, and hyphens.
- A tag can't start with a period or a hyphen.
- A tag is at most 128 characters long.
- Tags are case-sensitive, so
Betaandbetaare different tags.
Two properties of tags matter in practice.
First, a tag is mutable. Nothing stops you from pushing a different image under 1.0 tomorrow. The old image stays on the registry but loses that label. Treat latest and other moving tags as pointers, not guarantees.
Second, latest is only a default name. It doesn't mean "the newest image". Docker doesn't search for the most recent push. It applies the label latest only when you leave the tag out or tag it explicitly. If you push 1.0 and never push latest, then docker pull alice/myapp fails with a "not found" error, because that tag doesn't exist.
A common convention is to push several tags that point to the same image, for example 1.4.2, 1.4, and 1. Users who want stability pick the full version. Users who want patch updates pick a shorter one.
Repository names
Repository names have stricter rules than tags, and Docker Hub adds its own constraints on top of them.
- Use lowercase letters, digits, and separators. The separators are periods, hyphens, and underscores.
- Uppercase letters are rejected by the Docker CLI.
Alice/MyAppis an invalid reference. - On Docker Hub, the namespace must be your username or an organization you can write to, and it is also lowercase.
- You can use more path components with other registries (
registry.example.com/team/project/api), but Docker Hub uses exactly two: namespace and repository.
You can check a name without contacting any registry. docker tag validates its arguments locally:
docker pull alpine:3.20
docker tag alpine:3.20 Alice/MyApp:1.0The second command fails with an error like invalid reference format: repository name (Alice/MyApp) must be lowercase. Fix the name and run it again:
docker tag alpine:3.20 alice/myapp:1.0
docker images alice/myappDigests: naming an image by content
A tag is a label you choose. A digest is a fingerprint the registry computes from the image manifest, the small JSON document that lists the image's layers and configuration. A digest looks like this:
sha256:<64 hexadecimal characters>You reference an image by digest with an @ instead of a colon:
docker.io/library/alpine@sha256:<64 hexadecimal characters>Because the digest is derived from the content, it can't point at a different image later. Pulling by digest is the way to get exactly the bytes you tested. You can read the digest of an image you pulled or pushed:
docker pull alpine:3.20
docker image inspect alpine:3.20 --format '{{index .RepoDigests 0}}'The output has the form alpine@sha256:.... You can also list digests next to tags:
docker images --digests alpineIf you write both a tag and a digest (alpine:3.20@sha256:...), Docker pulls by digest and ignores the tag. The tag then serves only as a note to the reader. A later chapter covers how digests relate to layers and why repeated pushes are fast.
One image, many names
An image on your machine has an ID. Names are labels attached to that ID. Adding a name doesn't copy the image, and removing a name doesn't delete the image unless it was the last name.
docker pull alpine:3.20
docker tag alpine:3.20 alice/demo:1.0
docker tag alpine:3.20 alice/demo:stable
docker imagesThe listing shows three rows (alpine:3.20, alice/demo:1.0, alice/demo:stable) with the same IMAGE ID. This is why docker tag is fast and uses almost no disk space. The next chapters use this behavior: you build an image under a local name, then add a second name that includes your namespace, and push that second name.
Choosing names for your own images
A few habits keep names predictable and prevent mistakes.
- Build with the final name from the start. Running
docker build -t alice/myapp:1.0 .removes the need for a separate tagging step. Later in this tutorial you will tag after the build, because it shows how names work, but in daily use building with the final name is common. - Pin versions in tags. Prefer
1.0or a Git commit hash over relying onlatest. - Put the registry host first for non-Docker Hub registries, and always include it when you script against more than one registry.
- Keep repository names short and descriptive, with hyphens between words:
alice/invoice-service. - Never put secrets in a name. Image names show up in logs, process lists, and registry listings.
Common naming errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `invalid reference format` | Uppercase letters, a stray space, or a bad tag character. | Lowercase the repository and check the tag characters. |
| `denied: requested access to the resource is denied` on push | The name has no namespace, so Docker used `library`, or the namespace isn't yours. | Tag the image as `YOUR_USERNAME/repo:tag` and push that name. |
| `manifest unknown` or `not found` on pull | The tag doesn't exist, or the repository name is misspelled. | Check the tag list on Docker Hub and spell the name exactly. |
| `pull access denied for X, repository does not exist or may require 'docker login'` | A typo, or a private repository you aren't logged in for. | Verify the name, then log in if the repository is private. |
| Pushes go to the wrong registry | A hostname with no dot or port was read as a namespace. | Add a port or use a fully qualified hostname. |
Exercises
Exercise
Write the fully expanded form of the reference nginx that Docker uses when you run docker pull nginx.
Show answer
docker.io/library/nginx:latest. Docker adds the default registry (docker.io), the default namespace for single-word names (library), and the default tag (latest).
Exercise
For each of these references, say whether the first component is a registry host or a Docker Hub namespace: bob/web, registry.example.com/bob/web, localhost:5000/web, staging/web.
Show answer
bob/web: namespace. registry.example.com/bob/web: registry host, because it contains a dot. localhost:5000/web: registry host, because it is localhost and has a port. staging/web: namespace, because it has no dot or colon and isn't localhost.
Exercise
You build myapp:1.0 locally and run docker push myapp:1.0. The push fails with "requested access to the resource is denied". Explain why, and give the command that fixes the problem. Assume your Docker Hub username is alice and you are logged in.
Show answer
Without a namespace, Docker reads the name as docker.io/library/myapp:1.0, and only Docker's maintainers can write to library. Add a name that includes your namespace with docker tag myapp:1.0 alice/myapp:1.0, then run docker push alice/myapp:1.0.