Here’s a complete tutorial on docker tag
, covering what it does, examples, and use cases.
What is docker tag
?
docker tag
is a Docker command that creates a new tag (alias) for an existing Docker image. It does not create a new image but assigns a new name or version tag to an existing image ID. This is useful for version control, renaming images, or preparing images for deployment to a registry.
Key Features:
- Assign multiple tags to the same image.
- Rename images for better organization.
- Prepare images for publishing to Docker Hub or private registries.
- Helps with versioning and managing image lifecycle.
Basic Syntax
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Arguments:
SOURCE_IMAGE[:TAG]
: The existing image and optional tag you want to rename.TARGET_IMAGE[:TAG]
: The new name and optional tag for the image.
Examples of docker tag
1. Tag an Image with a New Version
docker tag my_app:latest my_app:v1.0
This tags the my_app:latest
image as my_app:v1.0
.
2. Add a Repository Name for Pushing to Docker Hub
docker tag my_app:latest myusername/my_app:latest
This prepares my_app:latest
for pushing to Docker Hub under myusername
.
3. Tag an Image with a Different Registry
docker tag my_app:latest registry.example.com/my_app:latest
This tags the image for pushing to a private registry (registry.example.com
).
4. Create Multiple Tags for the Same Image
docker tag my_app:latest my_app:stable
docker tag my_app:latest my_app:testing
This creates multiple tags (stable
and testing
) for the same image.
5. Tag an Image by ID
docker tag a1b2c3d4e5f6 my_app:v2.0
This tags the image with ID a1b2c3d4e5f6
as my_app:v2.0
.
6. Rename an Image
docker tag old_name:latest new_name:latest
This renames old_name:latest
to new_name:latest
.
7. Prepare an Image for Kubernetes Deployment
docker tag my_app:latest registry.example.com/project/my_app:v1.0.0
This tags the image for deployment to a Kubernetes cluster using a specific registry.
8. Use docker tag
in Automation Scripts
#!/bin/bash
docker tag my_app:latest my_app:backup_$(date +%Y%m%d)
echo "Image tagged as my_app:backup_$(date +%Y%m%d)"
This script tags the image with a timestamp.
9. Version Control with Semantic Tagging
docker tag my_app:latest my_app:1.0.0
docker tag my_app:latest my_app:1.0
docker tag my_app:latest my_app:1
This helps maintain semantic versioning (major.minor.patch
).
10. Tag a Multi-Architecture Image
If you are building for multiple architectures (linux/amd64
, linux/arm64
), tag each architecture-specific image:
docker tag my_app:arm64 my_app:latest-arm64
docker tag my_app:amd64 my_app:latest-amd64
Use Cases for docker tag
1. Version Control and Image Management
- Tag images with version numbers for easy reference and rollback.
- Example:
my_app:v1.0
,my_app:v2.0
.
2. Preparing Images for Deployment
- Add a tag for pushing images to Docker Hub or private registries.
- Example:
myusername/my_app:latest
.
3. Multi-Environment Workflows
- Tag images for different environments (development, staging, production).
- Example:
my_app:dev
,my_app:staging
,my_app:prod
.
4. Renaming Images
- Change image names for better organization or when switching repositories.
- Example: Rename
old_app:latest
tonew_app:latest
.
5. Automating CI/CD Pipelines
- Use
docker tag
in CI/CD pipelines to version and push images automatically. - Example: Tagging with Git commit hashes or build numbers (
my_app:build_123
).
6. Backup and Rollback
- Create backup tags before making changes, allowing for easy rollback.
- Example:
my_app:backup_20250207
.
7. Multi-Registry Support
- Prepare images for multiple registries (Docker Hub, AWS ECR, Google Artifact Registry).
- Example:
docker tag my_app:latest gcr.io/project/my_app:latest
.
List of Common docker tag
Commands
Command | Description |
---|---|
docker tag my_app:latest my_app:v1.0 | Tag my_app:latest as my_app:v1.0 |
docker tag my_app:latest myusername/my_app:latest | Add a repository name for Docker Hub |
docker tag my_app:latest registry.example.com/my_app:latest | Tag for a private registry |
docker tag old_image:latest new_image:latest | Rename an image |
docker tag a1b2c3d4e5f6 my_app:v2.0 | Tag an image by its ID |
docker tag my_app:latest my_app:backup_20250207 | Create a timestamped backup tag |
Best Practices for Using docker tag
:
- Follow semantic versioning (
major.minor.patch
) to maintain a clear image versioning strategy. - Use meaningful tags for different environments (
dev
,staging
,prod
). - Tag before pushing to a registry for clarity and traceability.
- Automate tagging in CI/CD pipelines for consistent version control.
- Avoid ambiguous tags like
latest
in production—always prefer versioned tags.
Common Errors and Solutions
- “No such image”
→ Ensure the source image exists. Usedocker images
to verify. - “Image is already tagged”
→ This is a warning, not an error. You can re-tag an image with different tags. - “Repository name must be lowercase”
→ Docker image names must be lowercase (e.g.,my_app
, notMy_App
).
Combining docker tag
with Other Commands
Push Tagged Images to Docker Hub
docker tag my_app:latest myusername/my_app:latest
docker push myusername/my_app:latest
Automate Versioning in CI/CD
docker build -t my_app:latest .
docker tag my_app:latest my_app:v1.0.0
docker push my_app:v1.0.0
Rollback to a Previous Version
docker tag my_app:v1.0 my_app:latest
docker run my_app:latest
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND