Sub topic 12
Tagging, pushing, and pulling
This chapter assumes you can build an image from a Dockerfile and run it with docker run. You should also know that an image is made of layers and has a digest. Here you will see how a local image gets a name that a registry understands, how it travels to the registry, and how it comes back.
Anatomy of an image reference
A reference tells Docker where an image lives and which version you want. The full form is:
[registry-host[:port]/]repository[:tag][@digest]Docker fills in defaults for anything you omit:
- No registry host means
docker.io(Docker Hub). - No tag means
latest. - A single-segment Docker Hub name such as
alpineexpands tolibrary/alpine.
So alpine, docker.io/library/alpine:latest and library/alpine all name the same thing.
| You type | Docker resolves it to |
|---|---|
| `alpine` | `docker.io/library/alpine:latest` |
| `myuser/myapp:1.0` | `docker.io/myuser/myapp:1.0` |
| `registry.example.com/team/myapp:1.0` | Registry `registry.example.com`, repository `team/myapp`, tag `1.0` |
| `localhost:5000/myapp` | Registry `localhost:5000`, repository `myapp`, tag `latest` |
Docker treats the first path segment as a registry host only if it contains a . or a :, or is exactly localhost. That is why team/myapp goes to Docker Hub, while registry.example.com/myapp goes to your own registry.
What a tag is
A tag is a movable label on a manifest, stored in a repository. It is not a copy of the image and it is not a version lock. Two consequences follow:
- One image can carry many tags.
1.0,1.0.3andlatestcan all point to the same digest. - A tag can be reassigned. If you push a different image as
1.0, the tag moves, and the old image becomes untagged. It still exists by digest until a cleanup removes it.
latest has no special meaning to the registry. It is only the default tag. It points to whatever was last pushed under that name.
Tagging a local image
docker tag adds a new name to an existing local image. It does not rebuild or copy any layers.
docker build -t myapp:1.0 .
docker tag myapp:1.0 registry.example.com/team/myapp:1.0
docker tag myapp:1.0 registry.example.com/team/myapp:latest
docker images --digests registry.example.com/team/myappAll three names show the same image ID. The registry hostname must be part of the tag, because the push command uses the name to decide where to send the image.
Logging in
Most registries require credentials, even for pulling private images. docker login stores them for the registry you name.
docker login registry.example.comFor scripts, pass the secret on standard input so it stays out of your shell history and process list:
echo "$REGISTRY_TOKEN" | docker login registry.example.com -u ci-bot --password-stdinCredentials are saved in ~/.docker/config.json. Without a credential helper they are only base64-encoded, which is not encryption. Configure a helper such as the OS keychain where you can, and prefer short-lived tokens over account passwords.
Pushing
Push the full name:
docker push registry.example.com/team/myapp:1.0Push is not a single upload. For each layer, the client asks the registry whether it already has a blob with that digest. Layers the registry already has are skipped, and the output shows Layer already exists. Missing layers are uploaded, then the config blob, and last the manifest. The tag only appears once the manifest is written.
Because blobs are addressed by digest, rebuilding an image with a small change only uploads the changed layers. Layers are also shared across repositories on the same registry, so pushing a second image on the same base is cheap.
To push every tag of a repository, use docker push --all-tags registry.example.com/team/myapp.
Pulling
docker pull registry.example.com/team/myapp:1.0Pull reverses the flow. The client asks for the manifest by tag, reads the layer digests, and downloads only layers it does not already hold locally, in parallel. It verifies each blob against its digest, then unpacks it. Pulling also prints the manifest digest, so you can record exactly what a tag resolved to.
docker run pulls automatically when the image is missing locally. It does not check for a newer version of a tag you already have. If myapp:latest exists locally, docker run myapp uses it even when the registry has moved on. Run docker pull explicitly, or use docker run --pull always, when you want the current content.
Pulling by tag is convenient but not reproducible. To get the same bytes every time, reference the digest, as covered in the digest-pinning chapter.
Typical errors
| Message | Usual cause |
|---|---|
| `denied: requested access to the resource is denied` | Not logged in, wrong namespace, or no push permission. On Docker Hub, a missing username prefix is common. |
| `no basic auth credentials` | `docker login` was not run for that registry host. |
| `An image does not exist locally with the tag: ...` | The name given to `docker push` does not match any local tag. Run `docker images` and check spelling. |
| `manifest unknown` or `not found` | The tag does not exist in that repository, or it was removed by cleanup. |
| `http: server gave HTTP response to HTTPS client` | A registry served over plain HTTP. Use TLS, or for a local test registry mark it insecure in the daemon configuration. |
A practical tagging scheme
A workable approach for most teams:
- Build once and tag with the Git commit, for example
myapp:3f9c2ab. - Add a release tag, such as
1.4.2, only after tests pass. - Move
latestor an environment tag, such asstaging, as a pointer to the current release.
Deployments should reference the commit or release tag, or the digest, never a moving tag alone. Keeping this scheme consistent also makes cleanup policies much simpler to write.
Exercise
You run docker build -t myapp . and then docker push myapp. The push fails with an access denied error. What is the cause, and what two commands fix it for a registry at registry.example.com and a repository team/myapp?
Show answer
The name myapp has no registry host, so Docker resolves it to docker.io/library/myapp and tries to push to Docker Hub, where you have no rights. Add a fully qualified tag and push that: docker tag myapp registry.example.com/team/myapp:1.0, then docker push registry.example.com/team/myapp:1.0. If you have not authenticated, run docker login registry.example.com first.
Exercise
Your team's Compose file says image: team/myapp:1.0. A colleague pushes a fixed build under the same tag, but your server keeps running the old code after docker compose up. Give two reasons this can happen and one way to avoid each.
Show answer
First, the old image is still cached locally, and starting a container does not re-check the registry. Run docker pull or use --pull always. Second, the tag is mutable, so two machines can hold different content under the same name. Publish a new immutable tag for each fix, or reference the image by digest.