Sub topic 14
Tagging Images and Publishing to a Registry
This chapter assumes you can build and run an image from a Dockerfile, as in the earlier chapters. You should also have a Docker Hub account or access to another registry. The goal is to give your image a meaningful name and version, then push it where other machines can pull it.
What a tag is
An image reference has this shape:
[registry-host/][namespace/]repository[:tag]- The registry host is where the image is stored. If you leave it out, Docker uses Docker Hub (
docker.io). - The namespace is usually your user or organization name.
- The repository is the name of the image, such as
myapp. - The tag is a label for one version. If you leave it out, Docker uses
latest.
A tag is a movable pointer to an image ID. It is not a version number that Docker enforces. latest is only a default name. It does not mean "newest", and Docker never updates it for you unless you build or push with that tag.
Tagging an image
You already built myapp:1.0. Add a second name that includes your namespace, so the registry knows where the image belongs:
docker tag myapp:1.0 yourname/myapp:1.0
docker tag myapp:1.0 yourname/myapp:1
docker tag myapp:1.0 yourname/myapp:latest
docker images yourname/myappdocker tag creates no new image. All three tags point to the same image ID, so no extra disk space is used. You can also tag at build time with docker build -t yourname/myapp:1.0 ..
A common scheme is to push a full version (1.0.3), a shorter alias (1.0, 1), and optionally latest. Deployments should use the full version or a Git commit hash. A tag such as latest can change under you.
Pushing to a registry
A registry is a server that stores images and serves them to docker pull. Docker Hub is the default. GitHub Container Registry, Amazon ECR, and others work the same way, but their image names start with a host name such as ghcr.io/yourname/myapp.
Log in, then push:
docker login
docker push yourname/myapp:1.0
docker push yourname/myapp:1
docker push yourname/myapp:latestFor a registry other than Docker Hub, pass the host name: docker login ghcr.io. Use an access token instead of your account password where the registry supports it.
Docker uploads the image as layers. If a layer already exists in the registry, Docker skips it. Pushing the second and third tags above is therefore almost instant, because they share every layer with the first.
Anyone with access can now run the image on another machine:
docker pull yourname/myapp:1.0
docker run --rm -d -p 3000:3000 yourname/myapp:1.0Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `denied: requested access to the resource is denied` | Not logged in, or the image name lacks your namespace | Run `docker login`, and tag the image as `yourname/myapp` |
| `An image does not exist locally with the tag` | The tag you are pushing was never created | Run `docker tag` first, and check the spelling with `docker images` |
| A server runs an old version | It pulled `latest` earlier and has a cached copy | Deploy a specific version tag, or run `docker pull` again |
| Push is very slow | The first push uploads every layer | Later pushes upload only changed layers |
Exercise
You run docker tag myapp:1.0 ghcr.io/acme/myapp:1.0 and then docker push ghcr.io/acme/myapp:1.0. Does Docker create a copy of the image when you run the tag command? Where does the push send the image?
Show answer
No. docker tag only adds another name that points to the same image ID, so no extra disk space is used. The push goes to GitHub Container Registry (ghcr.io), because the host name at the start of the reference selects the registry. You must be logged in to that registry with docker login ghcr.io.
Exercise
A teammate deploys yourname/myapp:latest on Monday and again on Friday, but the server still runs Monday's code after you pushed a fix. Give two possible causes and one change that prevents the problem.
Show answer
The server may have a cached local copy of latest and did not run docker pull, or the fix was pushed under a different tag and latest was never updated. Deploying an immutable version tag such as 1.0.4 (or a commit hash) prevents this, because each deployment names exactly the version it intends to run.