How to Tag Docker Image: Simple Syntax and Examples
Use the
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG] command to add a tag to a Docker image. Tags help you label images with versions or names for easy identification and use.Syntax
The basic syntax to tag a Docker image is:
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Explanation:
SOURCE_IMAGE[:TAG]: The existing image name and optional tag you want to tag.TARGET_IMAGE[:TAG]: The new name and tag you want to assign to the image.- If
:TAGis omitted, Docker useslatestby default.
bash
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Example
This example shows how to tag an existing Docker image named myapp with the tag v1.0 and also rename it to myrepo/myapp with the same tag.
bash
docker tag myapp myrepo/myapp:v1.0Common Pitfalls
Common mistakes when tagging Docker images include:
- Forgetting to specify the tag, which defaults to
latestand might cause confusion. - Using an incorrect source image name or tag that does not exist locally.
- Not pushing the newly tagged image to a remote registry if needed.
Always verify the image exists locally with docker images before tagging.
bash
docker tag myapp:wrongtag myrepo/myapp:v1.0 # This will fail if 'myapp:wrongtag' does not exist # Correct usage: docker tag myapp:latest myrepo/myapp:v1.0
Quick Reference
| Command | Description |
|---|---|
| docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG] | Add a new tag to an existing image |
| docker images | List all local Docker images and tags |
| docker push TARGET_IMAGE[:TAG] | Push tagged image to remote registry |
Key Takeaways
Use
docker tag to label images with meaningful names and versions.Always specify tags to avoid confusion; default is
latest if omitted.Verify the source image exists locally before tagging.
Tagged images can be pushed to registries for sharing or deployment.
Use
docker images to check your images and tags.