Docker Images
Estimated reading: 3 minutes 5 views

🚒 How to Tag Docker Images

Docker image tagging is an essential part of container development and deployment workflows. It helps you identify and manage versions of images, making it easier to track and deploy specific builds.

In this guide, we’ll explore how to tag Docker images, why tagging is important, and provide practical examples to help you get started.


🧠 What is Docker Image Tagging?

A Docker tag is a label applied to a Docker image to identify it. By default, if you don’t specify a tag, Docker will assign the latest tag.

The format to tag an image is:

<repository>:<tag>

For example:

nginx:1.25.3
  • nginx is the repository name.
  • 1.25.3 is the tag, representing a version.

πŸ› οΈ How to Tag Docker Images

You can tag Docker images using the docker tag command.

βœ… Syntax:

docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
  • SOURCE_IMAGE: The local image ID or name you want to tag.
  • TARGET_IMAGE: The new tag name (including repository and version/tag).

πŸ“Œ Example 1: Tagging a Local Image

Suppose you have built a local image called myapp:

docker build -t myapp .

Now tag it with a version:

docker tag myapp myusername/myapp:v1.0.0

This creates a new tag v1.0.0 for the same image under your Docker Hub namespace.


πŸ“Œ Example 2: Tagging with latest and dev

docker tag myapp myusername/myapp:latest
docker tag myapp myusername/myapp:dev

You can push these tags to a registry:

docker push myusername/myapp:latest
docker push myusername/myapp:dev

πŸ” Why Tagging Matters

Tagging helps in:

  • πŸ“¦ Version control of images
  • πŸ”„ Rollback and deployment management
  • πŸ“€ Publishing to Docker Hub or private registries
  • πŸ§ͺ Isolating dev, staging, and production builds

πŸ’‘ Best Practices for Docker Tags

  • Use semantic versioning: v1.0.0, v1.2.1
  • Tag with environment identifiers: dev, staging, prod
  • Avoid using latest in production pipelines unless explicitly tested
  • Maintain a changelog of tagged builds

❓ FAQ: Docker Image Tagging

1. What happens if I don’t specify a tag?

Docker will automatically assign the tag latest:

docker build -t myapp .  # This is the same as myapp:latest

2. Can I have multiple tags for the same image?

Yes! A single image can have multiple tags:

docker tag myapp myusername/myapp:1.0
docker tag myapp myusername/myapp:stable

Both refer to the same image ID.


3. How do I list all tags of a Docker image?

Docker CLI doesn’t directly list all tags from Docker Hub. Use a web browser or a tool like:

curl https://registry.hub.docker.com/v2/repositories/library/nginx/tags/

4. Can I change a tag after pushing it to Docker Hub?

No. You need to re-tag and push again:

docker tag myapp myusername/myapp:newtag
docker push myusername/myapp:newtag

Then optionally delete the old tag on Docker Hub.


5. How do I remove a local Docker tag?

Use:

docker rmi myusername/myapp:v1.0

Make sure the tag isn’t used by a container before deleting.


6. What is the difference between docker tag and docker build -t?

  • docker build -t: Tags an image during build.
  • docker tag: Adds a tag to an existing image.

Leave a Reply

Your email address will not be published. Required fields are marked *

Share this Doc

How to Tag Docker Images

Or copy link

CONTENTS
Scroll to Top