Sub topic 5
Tagging the Image for Your Repository
This chapter assumes you have Docker installed and running, you can build an image from a Dockerfile, and you have a Docker Hub account. You should already have a local image called myapp:1.0 from the earlier chapter, and you should know the namespace/repository:tag naming format. Here you will learn how docker tag connects a local image to a Docker Hub repository, which tagging habits are safe, and which mistakes cause push failures.
What `docker tag` actually does
docker tag does not copy an image and does not change it. It adds a second name, called a reference, that points to the same image ID. The image data on disk stays the same. Only the name table changes.
The syntax is:
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]The source is an image you already have locally. The target is the new name. If you leave out the tag on either side, Docker uses latest.
Docker decides where a push goes by reading the target name. The push command has no destination option. Whatever name you give it is the destination. That makes tagging the step that decides where your image goes.
Why the local name is not enough
Your image is called myapp:1.0. When Docker sees a name with no registry host and no namespace, it treats the name as a Docker Hub official image, which is an image in the library namespace. The full form of myapp:1.0 is:
docker.io/library/myapp:1.0You cannot push to library. It is reserved for images that Docker curates. If you try docker push myapp:1.0, Docker tries that path and the registry refuses with an access error. To push, the name must start with a namespace you own.
For a personal account, your namespace is your Docker Hub username. For an organization, it is the organization name.
Tagging for your repository
Run the tag command with your own username in place of YOUR_USERNAME:
docker tag myapp:1.0 YOUR_USERNAME/myapp:1.0
docker push YOUR_USERNAME/myapp:1.0The first command creates the new name. The second uploads the image to the repository YOUR_USERNAME/myapp on Docker Hub, under the tag 1.0. The push itself is covered in the next chapter. This chapter focuses on the tagging step and what you can check before pushing.
List your local images to see the result:
docker imagesYou should see two rows with the same image ID:
REPOSITORY TAG IMAGE ID CREATED SIZE
YOUR_USERNAME/myapp 1.0 3f1a9c2b7d10 5 minutes ago 7.8MB
myapp 1.0 3f1a9c2b7d10 5 minutes ago 7.8MBThe matching IMAGE ID shows that both names refer to one image. Removing one name with docker rmi leaves the other in place. Docker deletes the image data only when the last name is gone.
Tagging in one step with `docker build`
You do not need a separate docker tag command if you know the final name at build time. Use -t with the full name:
docker build -t YOUR_USERNAME/myapp:1.0 .You can pass -t more than once to apply several names during one build:
docker build -t YOUR_USERNAME/myapp:1.0 -t YOUR_USERNAME/myapp:latest .Use docker tag when the image already exists, for example when you built it earlier under a short name, pulled it from somewhere else, or want to promote a tested image to a new tag without rebuilding.
One image, several tags
A single image can carry as many tags as you want. A common pattern is to publish a precise version, a looser version, and a moving pointer:
docker tag myapp:1.0 YOUR_USERNAME/myapp:1.0.0
docker tag myapp:1.0 YOUR_USERNAME/myapp:1.0
docker tag myapp:1.0 YOUR_USERNAME/myapp:1
docker tag myapp:1.0 YOUR_USERNAME/myapp:latestThese four tags all share one image ID. Pushing each one uploads no extra data. The registry stores one set of layers and records four names for it. Layers are explained in a later chapter.
What tags mean in practice
A tag is a mutable label. Anyone with write access to the repository can move a tag to a different image at any time by pushing again. Docker does not stop you. This has consequences for how you choose tags.
| Tag style | Example | Behavior | Good for |
|---|---|---|---|
| Exact version | `1.0.0` | Should never move after release | Reproducible deployments |
| Minor or major version | `1.0`, `1` | Moves with each patch release | Users who want fixes automatically |
| `latest` | `latest` | Moves with each push that applies it | Quick trials, not production |
| Commit or build ID | `a1b2c3d` | Unique to one build | CI, rollbacks, traceability |
Two points are worth remembering.
First, latest is only a default name. It does not mean "the newest image in the repository". It is the tag Docker applies when you omit one. If you push 1.1.0 without also tagging it latest, then latest still points at the older image. Someone running docker pull YOUR_USERNAME/myapp gets the old one.
Second, because tags move, deployments that must be repeatable should reference either an exact version you never reuse, or a digest. A digest is a content hash that identifies one image permanently. Digests are covered in a later chapter.
Rules for valid names
Docker rejects names that break its format. The main rules for the repository part and the tag part are below.
| Part | Allowed characters | Other limits |
|---|---|---|
| Repository path (namespace and name) | Lowercase letters, digits, and separators `.`, `_`, `-` | Must be lowercase. Separators cannot start or end a component. |
| Tag | Letters (either case), digits, `_`, `.`, `-` | Up to 128 characters. Cannot start with `.` or `-`. |
The most frequent mistake is uppercase letters in the repository name. If your Docker Hub username has no uppercase letters, you are fine. Usernames on Docker Hub are lowercase, so copy yours exactly. A name such as YOUR_USERNAME/MyApp:1.0 fails with an invalid reference format error because MyApp contains uppercase letters.
Another common mistake is a stray character from the shell. A trailing space, an unset variable, or a missing variable can produce a name like /myapp:1.0, which is invalid. If you use variables, check them first:
DOCKERHUB_USER=YOUR_USERNAME
echo "Tagging as ${DOCKERHUB_USER}/myapp:1.0"
docker tag myapp:1.0 "${DOCKERHUB_USER}/myapp:1.0"Quoting the name keeps the shell from splitting it. Printing it first makes an empty variable obvious.
Your tag must match your login
The namespace in the tag has to be one that your logged-in account can write to. Two cases cause trouble.
- You tag with someone else's namespace, or a typo of your own. The push is refused with
denied: requested access to the resource is denied. - You belong to an organization and tag with your personal username instead of the organization name. The push goes to your personal repository, not the organization's.
Docker does not check this during docker tag. The command succeeds with any valid name. You only find out at push time. For this reason, read the target name before you push.
Checking your tags before pushing
Three commands help you confirm the state of things.
List all names for one repository:
docker images YOUR_USERNAME/myappConfirm that two names share an image by printing the ID for each:
docker image inspect --format '{{.Id}}' myapp:1.0 YOUR_USERNAME/myapp:1.0Both lines of output should be the same value.
Preview the full reference Docker will use for a short name by running:
docker image inspect --format '{{index .RepoTags 0}}' YOUR_USERNAME/myapp:1.0This prints one tag attached to the image. If the output is not what you expected, fix the tag before you push.
Retagging and removing names
To fix a wrong name, add the correct one and remove the wrong one:
docker tag myapp:1.0 YOUR_USERNAME/myapp:1.0
docker rmi YOUR_USERNAME/Myapp:1.0docker rmi on a name only removes that name when other names still point at the image. You will see output starting with Untagged:, and no image data is deleted.
If you tag a different image with a name that already exists, the name moves. The previous image keeps its image ID but loses that name, and it appears as <none> in docker images. This is called a dangling image. Run docker image prune to remove dangling images when you no longer need them.
Tagging for a registry other than Docker Hub
The same command works for other registries. You put the registry host at the start of the name:
docker tag myapp:1.0 registry.example.com/team/myapp:1.0Docker treats the part before the first / as a registry host if it contains a . or a :, or is localhost. Otherwise it treats it as a Docker Hub namespace. This is why YOUR_USERNAME/myapp goes to Docker Hub while registry.example.com/team/myapp goes elsewhere. For Docker Hub, the host docker.io is implied and you normally leave it out.
Summary of the tagging step
- Build or pull the image so it exists locally.
- Choose a namespace you can write to and a lowercase repository name.
- Pick a tag that says what the image is: an exact version, a build ID, or both.
- Run
docker tagor usedocker build -twith the full name. - Run
docker imagesto check the new name before pushing.
Exercise
You run docker push myapp:1.0 and get denied: requested access to the resource is denied. Explain why this happens, then give the two commands that fix it.
Show answer
Without a namespace, Docker reads myapp:1.0 as docker.io/library/myapp:1.0. The library namespace is reserved for Docker's official images, so the registry refuses the write. The fix is to add a name that begins with your own namespace and push that: docker tag myapp:1.0 YOUR_USERNAME/myapp:1.0 followed by docker push YOUR_USERNAME/myapp:1.0. You must also be logged in as that user.
Exercise
You push YOUR_USERNAME/myapp:2.0 but never tag it latest. A teammate runs docker pull YOUR_USERNAME/myapp. Which version do they get, and why?
Show answer
They get whichever image latest last pointed to, probably 1.0 or whatever was last pushed under that tag. latest is not computed from the newest push. It is an ordinary tag that Docker applies when no tag is given, and it only moves when someone tags and pushes it. To update it, run docker tag YOUR_USERNAME/myapp:2.0 YOUR_USERNAME/myapp:latest and push latest.