0
0
DockerHow-ToBeginner · 3 min read

How to Use Docker Tag Command: Syntax and Examples

Use the docker tag command to create a new tag for an existing Docker image. The syntax is docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG], which lets you rename or add tags to images for easier management and pushing to registries.
📐

Syntax

The docker tag command syntax is:

  • SOURCE_IMAGE[:TAG]: The existing image name and optional tag you want to tag.
  • TARGET_IMAGE[:TAG]: The new image name and optional tag you want to create.

If no tag is specified, latest is assumed by default.

bash
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
💻

Example

This example shows how to tag an existing image myapp:latest as myapp:v1.0 and then as username/myapp:v1.0 to prepare for pushing to Docker Hub.

bash
docker tag myapp:latest myapp:v1.0
docker tag myapp:v1.0 username/myapp:v1.0
⚠️

Common Pitfalls

Common mistakes when using docker tag include:

  • Forgetting to specify the tag, which defaults to latest and may cause confusion.
  • Using an incorrect source image name or tag that does not exist locally.
  • Not tagging images properly before pushing to a remote registry, causing push failures.

Always verify the source image exists with docker images before tagging.

bash
docker tag myapp myapp:v1.0  # Wrong if 'myapp' without tag doesn't exist
# Correct usage:
docker tag myapp:latest myapp:v1.0
📊

Quick Reference

CommandDescription
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]Create a new tag for an existing image
docker imagesList local Docker images to verify tags
docker push TARGET_IMAGE[:TAG]Push tagged image to remote registry

Key Takeaways

Use docker tag to create or rename image tags for easier management.
Always specify tags explicitly to avoid confusion with the default latest tag.
Verify the source image exists locally before tagging with docker images.
Tag images properly before pushing to remote registries to avoid push errors.