Sub topic 5
Tagging the Image for Your Repository
This chapter assumes you have Docker running, a local image built from a Dockerfile, and a Docker Hub account. You should also know the name format namespace/repository:tag from the previous chapter.
Why a local image can't be pushed yet
docker push decides where to send an image by reading the image's name. The registry host, namespace, and repository are all part of that name. A name like myapp:1.0 has no namespace, so Docker treats it as an official-style image and tries to push to docker.io/library/myapp. You don't own that namespace, and the push fails.
To fix this, give the image a second name that includes your namespace. That is what docker tag does.
What docker tag does
docker tag creates a new name that points to an image that already exists. It doesn't copy any data. Both names refer to the same image ID, so the extra name costs almost no disk space.
The syntax is:
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]The source is the name you built. The target is the name you want to push.
Worked example
Start with the Dockerfile and build from the earlier chapter:
FROM alpine:3.20
CMD ["echo", "Hello from Docker Hub"]docker build -t myapp:1.0 .
docker images myappNow add the Docker Hub name. Replace YOUR_USERNAME with your Docker Hub username. This is your namespace for a personal account.
docker tag myapp:1.0 YOUR_USERNAME/myapp:1.0
docker imagesThe output lists two rows with the same IMAGE ID:
| REPOSITORY | TAG | IMAGE ID |
|---|---|---|
| myapp | 1.0 | same ID |
| YOUR_USERNAME/myapp | 1.0 | same ID |
Because the IDs match, you have one image with two names.
Building with the final name directly
You can skip the separate tagging step by naming the image correctly at build time:
docker build -t YOUR_USERNAME/myapp:1.0 .You can also pass -t more than once to apply several names in one build. Use docker tag when the image already exists and you don't want to rebuild it.
Choosing tags
The tag after the colon identifies a version of the repository. If you leave it off, Docker uses latest. Keep these points in mind:
latestis only a default name. Docker doesn't update it automatically to mean "newest".- A tag is mutable. Pushing
1.0again replaces what1.0pointed to. - A specific tag such as
1.0lets others reproduce your image. Use one for anything people depend on. - One image can carry many tags, for example
1.0,1.0.3, andlatest.
docker tag myapp:1.0 YOUR_USERNAME/myapp:latestCommon mistakes
- Uppercase letters. Repository names must be lowercase. Docker Hub usernames are lowercase too, so
YOUR_USERNAMEshould be your real lowercase username. - Wrong namespace. If the namespace doesn't match your account or an organization you belong to, the push will be denied.
- Typos in the source name. The command
Error response from daemon: No such imagemeans the source name doesn't exist locally. Rundocker imagesto check. - Assuming tag copies data. Removing one name with
docker rmionly removes that name while the other still exists.
Summary of the step
- Confirm the local image with
docker images. - Run
docker tagwith your namespace, repository name, and a version tag. - Confirm both names appear with the same image ID.
The image is now named for your repository and ready to push once you are logged in.
Exercise
You built an image with docker build -t webapp:2.1 .. Your Docker Hub username is dana. Write the command that prepares it for pushing to the repository dana/webapp with the same version tag, and say how many copies of the image data exist on disk afterward.
Show answer
docker tag webapp:2.1 dana/webapp:2.1. Only one copy of the image data exists. The command adds a second name that points to the same image ID.