Sub topic 2
Reading and writing image names
This chapter assumes you have Docker installed, can use a terminal, and know how to build an image from a Dockerfile. You also need a free Docker Hub account. By the end you will be able to read any image name and write the one Docker Hub expects.
The parts of an image name
An image name is a reference that tells Docker where an image lives and which version you want. A full reference has up to four parts:
registry/namespace/repository:tag- Registry is the server that stores images. If you leave it out, Docker assumes Docker Hub (
docker.io). - Namespace is the account or organization that owns the repository. On Docker Hub this is your username.
- Repository is the collection of versions of one image, such as
myapp. - Tag is a label that picks one version inside the repository, such as
1.0. If you leave it out, Docker useslatest.
So YOUR_USERNAME/myapp:1.0 means: on Docker Hub, in the namespace YOUR_USERNAME, the repository myapp, the version tagged 1.0.
Docker reads the parts in a fixed way, shown below.
Short names and what Docker fills in
Docker expands short names before it contacts any server. The table shows what each form means.
| You write | Docker understands |
|---|---|
| `alpine` | `docker.io/library/alpine:latest` |
| `alpine:3.20` | `docker.io/library/alpine:3.20` |
| `YOUR_USERNAME/myapp` | `docker.io/YOUR_USERNAME/myapp:latest` |
| `YOUR_USERNAME/myapp:1.0` | `docker.io/YOUR_USERNAME/myapp:1.0` |
| `ghcr.io/acme/tool:2` | The registry `ghcr.io`, namespace `acme`, repository `tool`, tag `2` |
Official images such as alpine live in the special namespace library. Docker adds it for you, which is why they have no username in front.
Docker decides whether the first segment is a registry by looking for a dot, a colon, or the word localhost. ghcr.io has a dot, so Docker treats it as a registry host. YOUR_USERNAME has none of these, so it is a namespace on Docker Hub.
Why this matters for pushing
The name on your local image decides where docker push sends it. The Dockerfile from the previous chapter builds an image called myapp:1.0:
docker build -t myapp:1.0 .
docker images myappDocker reads myapp:1.0 as docker.io/library/myapp:1.0. You cannot push to library, because only Docker's official images live there. A push would be refused. The image needs a name that starts with your own namespace, such as YOUR_USERNAME/myapp:1.0. The next chapters cover how to add that name with docker tag.
Rules for valid names
Docker rejects references that break these rules:
- Repository names must be lowercase. They can contain letters, digits, and the separators
.,_, and-. - A tag can contain letters, digits,
_,., and-. It is at most 128 characters and cannot start with.or-. - Tags are case-sensitive, so
V1andv1are different tags.
Common mistakes
- Uppercase letters in the repository.
YOUR_USERNAME/MyAppfails withinvalid reference format. Usemyapp. - Forgetting the namespace. Pushing
myapp:1.0targetslibrary/myappand fails with an access error. - Misspelling the username. Docker will try to push to someone else's namespace. You get
denied: requested access to the resource is denied. - Relying on
latest. The tag is only a default name. It does not mean "newest". Use explicit version tags for anything you want to reproduce later.
Check your understanding
Exercise
You build an image with docker build -t webapp . and run docker push webapp. Which full reference does Docker use, and why does the push fail?
Show answer
Docker expands the name to docker.io/library/webapp:latest. The push fails because library is reserved for official images, and your account has no permission to write there. You need to tag the image as YOUR_USERNAME/webapp first.
Exercise
Is ghcr.io/acme/tool:2 hosted on Docker Hub? How can you tell?
Show answer
No. The first segment, ghcr.io, contains a dot, so Docker treats it as a registry host rather than a namespace.