Sub topic 3
Naming images: repositories and tags
An image name tells Docker where an image lives and which version of it you mean. This chapter covers how a reference is built, which defaults Docker fills in, and why a tag is a movable label and not a fixed identity.
It assumes you can run docker build and docker run. It also assumes you know that an image is built from layers, as covered earlier. Pushing and pulling come in a later chapter. Here we only name things.
Anatomy of an image reference
A full image reference has up to four parts:
registry.example.com:5000/team/myapp:1.0@sha256:<digest>| Part | Example | Meaning |
|---|---|---|
| Registry host | `registry.example.com:5000` | The server that stores the image. The port is optional. |
| Repository path | `team/myapp` | A named collection of related image versions. |
| Tag | `1.0` | A human-readable label for one version in the repository. |
| Digest | `sha256:...` | A content hash that identifies exactly one image. |
The registry host, tag, and digest are all optional. The repository path is the only required part.
The terms are easy to mix up:
- Repository: all versions of one image, such as
team/myapp. - Image name: usually the same thing as the repository, sometimes with the registry host in front.
- Tag: a label that points to one version inside a repository.
What Docker fills in for you
When you write a short name, Docker expands it before it does anything else. These three references mean the same image:
nginx
nginx:latest
docker.io/library/nginx:latestTwo defaults apply:
- No tag means
latest. Docker adds:latestfor you. - No registry means Docker Hub. Docker adds
docker.io. A single-component name such asnginxalso gets thelibrary/namespace, which Docker Hub uses for official images.
Docker decides whether the first path component is a registry host with one rule. If it contains a . or a :, or is exactly localhost, it is a host. Otherwise it is part of the repository path on Docker Hub.
myuser/myappis a repository on Docker Hub.myuseris an account or organization.registry.example.com/myappuses a private registry.localhost:5000/myappuses a registry on your own machine.registry/myappis a Docker Hub repository, becauseregistryhas no dot or colon. This surprises people who expect a private host.
Naming rules
Repository names must follow these rules:
- Each path component is lowercase letters and digits, with separators between them.
- Allowed separators are a period, one or two underscores, or one or more dashes.
- Components are joined with
/.
MyApp is invalid because of the capital letters. team/my-app and team/my_app are valid.
Tags have different rules:
- Allowed characters are letters, digits, underscores, periods, and dashes.
- A tag cannot start with a period or a dash.
- A tag can be at most 128 characters.
Tags can contain uppercase letters. Version tags such as 1.0, 1.0.3, and 2024-06-01 are common. So are tags that carry variant information, such as 1.0-alpine.
A tag is a movable pointer
A tag names a version, but the registry can repoint it at any time. The tag is not part of the image content. It is a label that currently points at one image.
This behavior causes several problems:
myapp:latesttoday andmyapp:latestnext month can be different images.latestis not "the newest build" by any special rule. It is the default tag name, and it points wherever someone last put it.- Two people can push different images to
myapp:1.0. The last push wins, and the earlier image loses that tag.
Moving a tag is routine. Many projects move 1.4 to the newest 1.4.x patch release on purpose, so users get fixes by pulling again. The same habit becomes a risk when a build or deployment needs a repeatable result.
One image, several names
You can give one image several tags without copying any data. docker tag adds a new name that points at the same image ID:
docker build -t myapp:1.4.2 .
docker tag myapp:1.4.2 myapp:1.4
docker tag myapp:1.4.2 myapp:1
docker tag myapp:1.4.2 myapp:latest
docker image ls myappThe docker image ls output shows four rows with the same IMAGE ID. This layered scheme is common:
1.4.2is precise and rarely moves.1.4moves with each patch release.1moves with each minor release.latestmoves with every release.
Consumers choose how much change they accept by choosing how specific a tag they use.
When you tag a build with a name that already exists, Docker moves the tag to the new image. The old image keeps its layers but loses the name. It then appears as <none> in docker image ls and is called dangling:
docker build -t myapp:dev .
# edit a source file, then build again
docker build -t myapp:dev .
docker image ls --filter dangling=trueThe second build takes the myapp:dev tag away from the first image. Cleaning up dangling images comes in a later chapter.
Tags and digests together
A digest identifies content exactly, which a tag cannot do. You can show it with --digests:
docker image ls --digests myappA reference can contain both a tag and a digest:
myapp:1.4.2@sha256:<digest>When both are present, the digest decides which image is used. The tag serves as a readable note for people. Later chapters cover digests and how to pin by digest.
Choosing a tagging scheme
For a team, a few habits prevent most confusion:
- Tag every release with a specific version, and treat that tag as immutable by convention.
- Add a tag from the source revision, such as a short Git commit hash like
myapp:3f2a9c1. It links a running image back to its code. - Do not deploy
latestto production. You cannot tell from the name which version is running. - Keep tag names lowercase and predictable so scripts can match them.
Registries differ on whether tags can be locked. Some can mark a tag immutable, so a second push to the same tag fails. Check what your registry offers instead of assuming it.
Common errors
invalid reference format: The name has uppercase letters in the repository, a bad character in the tag, or a stray space. Check the rules above.pull access deniedorrepository does not exist: The reference expanded to a different registry or namespace than you meant. Rundocker image lsor write out the fulldocker.io/...form to see what Docker is using.- Wrong image after rebuild: A tag moved but a container still runs the old image. A running container keeps the image it started from. Recreate the container to use the new one.
Exercise
You run docker tag myapp:1.4.2 registry/myapp:1.4.2, intending to name an image for your company registry at the host registry. Where does Docker think the image belongs, and how would you fix the name?
Show answer
Docker treats registry as a Docker Hub namespace, because the first component has no . or : and is not localhost. The reference means docker.io/registry/myapp:1.4.2. To target a host, use a name that is recognizable as a host, such as registry.example.com/myapp:1.4.2, or add a port as in registry:5000/myapp:1.4.2.
Exercise
You tag build output as myapp:1.4.2, myapp:1.4, and myapp:latest. A week later you build and tag a new patch as myapp:1.4.3 and move myapp:1.4 and myapp:latest to it. What happens to the image tagged 1.4.2, and which tag should a production deployment use if it must keep running exactly the first build?
Show answer
The 1.4.2 image keeps its tag and is unchanged, because only 1.4 and latest were moved. Production should use myapp:1.4.2, or better a digest reference. 1.4 and latest now point at the newer image.