Sub topic 6
Pushing the image
You have a local image, a logged-in CLI, and a second tag that carries your Docker Hub namespace. This chapter covers the step that uploads the image: docker push. It explains what the command does, how to read its output, which options matter, and what the push changes on Docker Hub.
This chapter assumes you can build an image from a Dockerfile and have run docker login successfully. It also assumes that docker tag has already created the name YOUR_USERNAME/myapp:1.0, as in the previous chapters. Replace YOUR_USERNAME with your own Docker Hub username throughout.
The command
docker push YOUR_USERNAME/myapp:1.0The argument is a name and tag, not an image ID. Docker looks up that name in your local image store and uploads whatever it points to. If the name does not exist locally, the command fails before any network traffic. If the name lacks a registry host, Docker assumes docker.io, which is Docker Hub.
The name decides where the image goes. The CLI reads it in three parts:
| Part of the name | In the example | Meaning |
|---|---|---|
| Registry host | `docker.io` (implied) | The server that receives the upload |
| Repository path | `YOUR_USERNAME/myapp` | The namespace and the repository |
| Tag | `1.0` | A label inside that repository |
If you leave out the namespace and push myapp:1.0, Docker reads it as docker.io/library/myapp. The library namespace belongs to Docker's official images, so your credentials cannot write there and the push is denied. Tagging with your namespace first is what prevents this.
What happens during a push
A push is a conversation between the Docker client and the registry. The client does not send the image as one file. It sends the image's layers, then a small document that lists them.
In more detail:
- Authorization. The client reads your stored credentials, either from
~/.docker/config.jsonor from a credential helper such as your operating system's keychain. It exchanges them for a short-lived token that allows writing to the target repository. - Layer check. The client sends a lightweight request for each layer. If the registry already holds that layer, the client skips the upload.
- Layer upload. Missing layers are sent as compressed blobs. Layers upload in parallel, by default up to five at a time.
- Manifest upload. After all layers are present, the client uploads the manifest. The manifest lists the layers and the image configuration. Only at this point does the tag
1.0start to point at the image.
The order in step 4 is important. A push that is interrupted during layer upload leaves no visible tag change, because the manifest was never written. Running the same command again finds the layers that already arrived and uploads only the rest.
Reading the output
With the small Alpine-based image from earlier chapters, a first push looks like this. Your digest and size will differ.
The push refers to repository [docker.io/YOUR_USERNAME/myapp]
a1b2c3d4e5f6: Mounted from library/alpine
1.0: digest: sha256:3f5a0c9e7d1b2a4c6e8f0a1b3c5d7e9f1a2b4c6d8e0f1a3b5c7d9e1f2a4b6c8d size: 528Each line has a meaning:
The push refers to repository [...]shows the full name Docker resolved. Check it first. If the path is not what you intended, stop and fix the tag.- A layer ID followed by a status. Layers go through states such as
Preparing,Waiting,Pushing, andPushed. Layer already existsmeans the registry already holds that layer in your repository, so nothing was uploaded.Mounted from ...means the registry already holds the layer in another repository and linked it into yours without a transfer. Base-image layers often show this. Whether you seeMounted from library/alpineorLayer already existsdepends on the registry's state, and neither indicates a problem.- The final line has the tag, the manifest digest, and the manifest size in bytes. The digest is the permanent identifier of what you just pushed. Copy it if you want to refer to this exact version later.
In an interactive terminal the layer lines update in place. In CI logs and when output is piped, they print as separate lines.
Tags move
A tag is a pointer. It is not a stored copy. Pushing the same name again after rebuilding the image moves the tag to the new manifest. Anyone who pulls YOUR_USERNAME/myapp:1.0 afterward gets the new content. The old content remains reachable only by its digest.
This has practical consequences:
- Treat tags such as
1.0as promises you choose to keep. If a version has been released, do not push a different image under it. - Treat
latestas a convention and not as a guarantee. It is only a tag calledlatest. It is not "the newest push" unless you make it so. - If you must be certain which image a deployment runs, reference the digest:
YOUR_USERNAME/myapp@sha256:....
If you push without a tag, Docker pushes latest:
docker push YOUR_USERNAME/myappThis fails with a "tag does not exist" error if you never created a local latest tag. To push every local tag of a repository in one command, use --all-tags:
docker tag myapp:1.0 YOUR_USERNAME/myapp:latest
docker push --all-tags YOUR_USERNAME/myappThe -a flag is the short form. The command uploads each shared layer once and writes one manifest per tag, so adding a tag is cheap.
Useful options
| Option | Effect |
|---|---|
| `--all-tags`, `-a` | Push every local tag of the repository |
| `--quiet`, `-q` | Suppress progress output and print only the final result |
| `--disable-content-trust` | Skip image signing for this push, when content trust is enabled in your environment |
Quiet mode is helpful in scripts because progress bars add noise to logs. The command still exits with a non-zero status on failure, so you can chain it:
docker push --quiet YOUR_USERNAME/myapp:1.0 && echo "push succeeded"What the first push creates
If the repository YOUR_USERNAME/myapp does not exist yet, Docker Hub creates it when the first push arrives. This works only in a namespace you can write to, which is your own username or an organization where you have permission. After your first push, open the repository on Docker Hub and check its visibility. The next chapters cover how visibility works and how to change it. Do not assume the default is right for what you are uploading.
Pushing from a clean state
A useful habit is to run the whole sequence from scratch at least once, so you know it does not depend on leftovers on your machine:
docker build -t myapp:1.0 .
docker tag myapp:1.0 YOUR_USERNAME/myapp:1.0
docker push YOUR_USERNAME/myapp:1.0Because myapp:1.0 and YOUR_USERNAME/myapp:1.0 point to the same image ID, docker images myapp and docker images YOUR_USERNAME/myapp list the same image under two names. Pushing one name does not push the other. You can confirm both names share an ID with this command:
docker images --format "{{.Repository}}:{{.Tag}} {{.ID}}" | grep myappCommon first-time mistakes
- Pushing the short name.
docker push myapp:1.0targetslibrary/myappand is denied. Push the namespaced name. - Forgetting to log in. Without credentials, the push fails with an authentication or "access denied" message. Run
docker loginfirst. - Using the wrong account. If you are logged in as one user and push to another user's namespace, the registry rejects the write. Check the username in the name against the one you logged in with.
- Typing a tag that does not exist locally. Docker reports that the tag does not exist, and nothing is sent. Run
docker imagesand compare.
The next chapters cover the verification step and a full error reference. For now, keep the rule that the push succeeded when you see the final digest: line and the command exits with status 0. You can check the status right after the push:
docker push YOUR_USERNAME/myapp:1.0
echo "exit status: $?"Exercise
You run docker push myapp:1.0 while logged in as alice, and Docker Hub denies the request. Explain why, and give the two commands that fix it.
Show answer
Without a namespace, Docker resolves the name to docker.io/library/myapp. The library namespace is reserved for official images, so alice cannot write to it. Add the namespace with a second tag, then push that name: docker tag myapp:1.0 alice/myapp:1.0 and docker push alice/myapp:1.0.
Exercise
You push YOUR_USERNAME/myapp:1.0, rebuild the image with a code change, tag it YOUR_USERNAME/myapp:1.0 again, and push again. What happens to the first image, and how could a user still pull it?
Show answer
The tag 1.0 is moved to the new manifest, so pulling myapp:1.0 returns the new image. The first image is no longer reachable by that tag, but it can still be pulled by its manifest digest, for example YOUR_USERNAME/myapp@sha256:<old digest>, if the user recorded it.