Sub topic 11
Retagging with docker tag and pushing to a registry
This chapter assumes you can run docker build -t, that you know an image has a repository name and a tag, and that you have an account on a registry. A registry is a server that stores images and serves them to other machines.
Tags are names, not copies
An image has one content-based ID. A tag is a label that points to that ID. docker tag adds another label to an image that already exists. It does not copy layers or rebuild anything, so it finishes instantly and uses no extra disk space.
docker tag myapp:1.0 registry.example.com/team/myapp:1.0
docker push registry.example.com/team/myapp:1.0The first command adds a registry-qualified name to the local image. The second uploads that tag.
How a full image name is read
Docker decides where to push by reading the name before the last slash.
| Part | Example | Meaning |
|---|---|---|
| Registry host | `registry.example.com` | Server that receives the push |
| Repository path | `team/myapp` | Namespace and image name |
| Tag | `1.0` | Version label |
If you leave out the host, Docker assumes Docker Hub. If you leave out the tag, Docker assumes latest. So docker push myapp tries to push docker.io/library/myapp:latest, which usually fails with a permission error because you do not own the library namespace.
Logging in
Most registries require authentication before a push.
docker login registry.example.comDocker prompts for a username and password or access token, then stores a credential for that host. Use a token with push permission only, rather than your account password.
What happens during a push
Docker uploads each layer as a separate blob. Before sending a layer, it asks the registry whether that layer already exists. Layers already present are skipped, and the output shows Layer already exists. This is why pushing a second tag of the same image is fast.
Pushing several tags
Tag the image once for each name you want to publish, then push each one.
docker tag myapp:1.0 registry.example.com/team/myapp:1.0
docker tag myapp:1.0 registry.example.com/team/myapp:latest
docker push registry.example.com/team/myapp:1.0
docker push registry.example.com/team/myapp:latestTo push every tag of a repository in one command, add --all-tags:
docker push --all-tags registry.example.com/team/myappTake care with this option. It sends every local tag in that repository, including old or experimental ones.
Common errors
denied: requested access to the resource is denied: You are not logged in, your account lacks push rights, or the repository path is wrong. Check the name withdocker image lsand rundocker loginagain.An image does not exist locally with the tag: You pushed a name you never created. Rundocker tagfirst.- Pushed to the wrong place: The name had no host, so Docker used Docker Hub. Always include the registry host for private registries.
- A tag now points to the wrong image: Pushing an existing tag replaces what it points to on the registry. Most tags are mutable, so anyone pulling that tag later gets the new content.
Exercise
You built myapp:2.0 locally. Write the commands to publish it to registry.example.com/team/myapp as both 2.0 and latest. Does the second tag upload the layers again?
Show answer
Run docker tag myapp:2.0 registry.example.com/team/myapp:2.0, then docker tag myapp:2.0 registry.example.com/team/myapp:latest, then push each name with docker push. The second push does not upload the layers again. The registry already has them, so Docker reports Layer already exists and only sends the tag reference.