Sub topic 12
Tagging, pushing, and pulling
This chapter assumes you can run docker build and docker run, and that you know what a layer is and how a repository and tag name an image. It covers moving an image between your machine and a registry. Authentication is covered in the next chapter, so here we assume you are already logged in or the registry is open.
Anatomy of an image reference
A fully qualified reference has four parts: registry.example.com/team/myapp:1.0.
| Part | Example | If omitted |
|---|---|---|
| Registry host | `registry.example.com` | Docker Hub (`docker.io`) |
| Repository path | `team/myapp` | Required |
| Tag | `1.0` | `latest` |
| Digest | `@sha256:...` | Not used |
The registry host decides where docker push sends the image. This is why a name like myapp:1.0 cannot be pushed to your own registry. Docker would try to send it to Docker Hub.
Tagging
docker tag does not copy data. It adds another name that points to the same image ID.
docker tag myapp:1.0 registry.example.com/team/myapp:1.0
docker tag myapp:1.0 registry.example.com/team/myapp:latestBoth new names now refer to the image you built. Run docker images and you will see the same image ID on every line.
Pushing
Push by the full name. Docker uploads only the layers the registry does not already have.
docker push registry.example.com/team/myapp:1.0The output lists each layer with a status such as Pushed or Layer already exists. A second push of a slightly changed image is fast, because shared base layers are skipped. Finally, the registry stores the manifest and attaches the tag to it.
Pulling
Pull downloads the manifest first, then fetches only the layers missing from your local store.
docker pull registry.example.com/team/myapp:1.0Common errors
denied: requested access to the resource is denied: You are not logged in, or the repository path is not one you may write to. On Docker Hub, the path must start with your account or organization name.- Pushing goes to the wrong place: The image name lacks the registry host. Re-tag with the full name.
manifest unknownornot foundon pull: The tag does not exist. Check for typos, and check that the push finished.- Unexpected image after
docker pull myapp: With no tag, Docker pullslatest. A tag is a movable label andlatesthas no special meaning beyond being the default.
Tagging strategy
Push more than one tag for a release, for example 1.0.3, 1.0, and a Git commit hash. The commit tag lets you trace a running image to its source. The short tag 1.0 moves forward with each patch release.
Exercise
You run docker build -t myapp:2.0 . and then docker push myapp:2.0 to your company registry at registry.example.com. The push fails or goes to Docker Hub. What is wrong, and what two commands fix it?
Show answer
The image name has no registry host, so Docker treats it as a Docker Hub name. Fix it with docker tag myapp:2.0 registry.example.com/team/myapp:2.0 followed by docker push registry.example.com/team/myapp:2.0.