Image Tags & Versions

Tags label different versions of an image, and the special latest tag is only a default, not a promise of the newest version.

Syntaxdocker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

An image reference has the form repository:tag, for example nginx:1.27. The tag identifies a specific version of the image.

The latest tag

If you do not specify a tag, Docker assumes latest. This is just a default label — it does not automatically mean the most recent build. Relying on latest in production can lead to surprises.

Retagging

You can add extra tags to an existing image with docker tag. One image can have many tags pointing at it.

Full image names

A complete reference can include a registry host, like ghcr.io/acme/api:2.1. Without a host, Docker Hub is assumed.

Example

Example · bash
# Give an existing image an additional tag
docker tag myapp:1.0 myapp:latest

# Tag for a specific registry before pushing
docker tag myapp:1.0 ghcr.io/acme/myapp:1.0

# List all tags stored locally
docker images myapp

When to use it

  • A release engineer tags the same built image as both v2.3.1 and latest before pushing, so existing users and new pullers both get the right version.
  • A team pins their base image to node:20.14.0-alpine3.19 instead of node:latest to prevent surprise breakages from upstream updates.
  • An ops team stores images with git commit SHA tags so any production deployment can be traced back to the exact source commit.

More examples

Tag an image with a version

Builds the image with an explicit semantic version tag, then creates a latest alias pointing to the same image.

Example · bash
docker build -t myapp:2.3.1 .
docker tag myapp:2.3.1 myapp:latest

Tag with registry and push

Prefixes the tag with the registry hostname so docker push sends the image to the correct private registry.

Example · bash
docker tag myapp:2.3.1 registry.example.com/myorg/myapp:2.3.1
docker push registry.example.com/myorg/myapp:2.3.1

Tag with git commit SHA

Uses the current git commit hash as a tag so every image is traceable back to the exact source revision.

Example · bash
GIT_SHA=$(git rev-parse --short HEAD)
docker build -t myapp:${GIT_SHA} .
docker tag myapp:${GIT_SHA} myapp:latest

Discussion

  • Be the first to comment on this lesson.