Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The Command

docker tag SOURCE[:TAG] TARGET[:TAG]

docker tag myapp:latest myapp:1.4.0
docker tag myapp:1.4.0 registry.example.com/shop/myapp:1.4.0

Run docker images myapp afterward: the new names show the same IMAGE ID as the source. Nothing is copied and no disk space is used. The listing is explained in docker images.

How a Full Image Name Is Built

registry.example.com:5000/shop/myapp:1.4.0
PartExample
Registry hostregistry.example.com:5000
Namespaceshop
Repositorymyapp
Tag1.4.0

Docker treats the first part as a registry host only if it contains a dot or a colon, or is localhost. Otherwise the name refers to the default registry, and a one-part name such as nginx means the official image of that name there. When the tag is left out, Docker uses latest. Repository names are lowercase; a tag may hold up to 128 letters, digits, underscores, periods, and dashes, and may not start with a period or dash. Names that break these rules fail with the error described in Docker invalid reference format.

Why Not to Deploy by latest

latest is only the default name. It does not mean "newest" unless whoever pushes keeps it that way, and it moves with every push. Two servers that pull at different times can run different builds under the same name, and when something breaks there is no name for the version to go back to. Deploy a fixed version tag, and treat latest as a convenience for people trying the image.

Tag at Build Time

docker build accepts several -t options, so one build gets all its names at once. A habit that works well is the release version plus the short Git commit:

VERSION=1.4.0
COMMIT=$(git rev-parse --short HEAD)
docker build -t registry.example.com/shop/myapp:$VERSION \
  -t registry.example.com/shop/myapp:$VERSION-$COMMIT .

The version tells people what changed; the commit tells you exactly which code is inside. Building images is covered in Dockerfile: how to write one, and with Compose in docker compose build.

Tag and Push to a Private Registry

docker login registry.example.com
docker tag myapp:1.4.0 registry.example.com/shop/myapp:1.4.0
docker push registry.example.com/shop/myapp:1.4.0
docker push --all-tags registry.example.com/shop/myapp

The registry host in the name is how Docker knows where to push. An image named only myapp cannot be pushed to a private registry until it gets a name that includes the host. Signing in with a token is covered in docker login.

Tags Can Move: Pin by Digest

Anyone with push rights can point 1.4.0 at a different build, and base image tags such as nginx:alpine move on purpose whenever a fix is published. A digest cannot move:

docker images --digests nginx
docker pull nginx@sha256:DIGEST

Replace DIGEST with the hash from the first command. The same form works in a Compose file and a Kubernetes manifest. For an image in a private registry, write the full name without the tag, then the at sign, then sha256: and the hash. A pinned digest never receives fixes by itself, so update it on purpose, for example together with a version bump.

Remove a Tag

docker rmi myapp:1.4.0-old

If other names point to the same image, Docker answers with "Untagged" and keeps the image. The image is deleted only when its last name is removed and no container uses it. Deleting a tag in a registry is done through that registry's interface or API, and not every registry allows it.

Was this answer helpful?
Loading...