Sub topic 3
Naming images: repositories and tags
This chapter assumes you can run docker run, know what a container is, and have Docker installed. You should also know that an image is built from layers, which the previous chapter covered.
Anatomy of an image reference
An image reference is the string you pass to docker pull, docker run, or a FROM line. A fully qualified reference has four parts:
registry.example.com:5000/team/myapp:1.0| Part | Example | Meaning |
|---|---|---|
| Registry host | `registry.example.com:5000` | The server that stores the image. A port is optional. |
| Repository path | `team/myapp` | A named collection of related images. |
| Tag | `1.0` | A human-readable label for one version. |
| Digest | `@sha256:...` | A content hash that identifies exact bytes. |
A repository is a set of images that share a name, such as myapp. A tag is a mutable label that points to one image in that repository. The digest is covered in a later chapter.
What Docker fills in for you
Short names are shorthand. Docker expands them using defaults.
- If you omit the registry, Docker assumes Docker Hub (
docker.io). - If you omit the tag, Docker assumes
latest. - Official images on Docker Hub live under the
library/namespace.
So these commands refer to the same image:
docker pull alpine
docker pull docker.io/library/alpine:latestTags are labels, not versions
A tag is a pointer. The registry can move it to a different image at any time. One image can also carry several tags, and you can add tags without rebuilding:
docker build -t myapp:1.0.3 .
docker tag myapp:1.0.3 myapp:1.0
docker tag myapp:1.0.3 myapp:1
docker tag myapp:1.0.3 myapp:latest
docker images myappAll four names show the same image ID. A common scheme is to publish a precise tag (1.0.3) that you never move, plus broader tags (1.0, 1) that you move forward on each release.
Common errors
- Uppercase letters in a repository name. Repository names must be lowercase.
docker build -t MyApp .fails. - A missing registry prefix on push.
docker push myapp:1.0tries to push to Docker Hub. To push elsewhere, tag the image with the registry host first. - A tag pointing to a changed image. If a deployment behaves differently after a re-pull, the tag may have moved. Use unique tags per build, or reference by digest.
- Confusing the port with the tag. In
localhost:5000/myapp, the colon after the host is a port. Inmyapp:5000, it is a tag.
Exercise
You run docker tag myapp:1.0.3 registry.example.com/team/myapp:1.0.3. Does this copy the image, and what is the repository name in the new reference?
Show answer
No, it does not copy any layers. It adds a second name that points to the same image ID. The repository is team/myapp on the registry registry.example.com, and the tag is 1.0.3.