How to Tag a Docker Image

Image tagging is a recommended practice to store and keep the images in a registry. This is the most important chapter and we should learn how to tag a Docker image in the first place. Docker image tagging helps you group the images with tags before you push them to the registry. The registry can be Docker Hub, AWS ECR, or Google Container Registry. An image tag can contain lowercase and uppercase letters, digits, underscores, periods, and dashes. 

So, that was a general overview of the Docker tag. Let’s learn how to tag a Docker image practically with docker tag command.

1. The docker tag command

Earlier, we pulled some images from the Docker Hub and learned how pull works. Take the following example of an NGINX image.

[email protected]:~$ docker images
nginx latest 8a0f0f376e66 40 hours ago 179MB

If we break it down, we can see the following values of the image.

  • Repository; where the image is stored publically, here it is “nginx”.
  • Tag; the tag can be a version or any other name, it’s “latest” here.
  • Image ID; the unique ID of the image
  • Created; time when the image was created.
  • Size; the size of the image.

Following is the general syntax of the docker tag command:

docker tag source_image:tag target_image:tag

The source image locally on the system is nginx:latest. We want to change the tag to 1.1 instead of “latest”. Let’s assume it would be the first version of the webserver. 

Here is the docker tag command for that:

docker tag nginx:latest nginx:1.1
  • Source image: nginx:latest
  • Target image: nginx:1.1

Now see the following image showing the images on our system. The docker command does not rename the tag only. It creates a new image with a new tag. You can see the image ID is the same for both images.

docker tag image

2. Tag a Docker Image with Repository and Tagname

While working on the Docker Hub, we created a public and a private repository. You can see the two repositories that I created on Docker Hub. To push an image into a registry, you must name the image similar to the repository name. 

Here, the public repository name is softwarespice/devimages, right?

Therefore, the image must have this repository as its name. You can set a tag of your choice.

docker hub repos

That’s how we’ll execute the command.

docker tag nginx:latest softwarespice/devimages:image1


docker tag ubuntu:20.04 softwarespice/devimages:image2

Run the docker images command and you should see the new images with updated names and tags. 

3. Tag a Docker Image by Image ID

Each image has an image ID. When we run the docker images command, we can see the IDs of the respective images. We can give an image a name and a tag using its ID.

Let’s suppose the image ID of alpine:latest is  d4ff818577bc . I will copy the image ID and run the docker tag command.

That’s how the command should look:

docker tag image_ID image_name:tag

We’ll name and tag this image as alpine:new.

docker tag d4ff818577bc alpine:new

The new image will be updated when you run the docker images command.

[email protected]:~$ docker images
alpine new d4ff818577bc 3 weeks ago 5.6MB

This is an ideal command to use when the images are pulled with tags.

4. A Quick Summary

We are going to use the docker tag command a lot in the coming articles of pushing and pulling the Docker images. Without proper names and tags, you will not be able to push or pull the images in the public or private repositories. Image tagging is intensively used in code builds especially when you are using CI/CD. In continuous integration, each docker build updates the image tag. Each time in continuous deployment, that image with the updated or latest tag is deployed. 

I hope now you know how to tag a Docker image. In case of any issues or questions, feel free to let me know in the comments section below.

Finally, if you are looking forward to learning Docker from the beginning, please go through the following basic guides:

Happy Docking!!!

Leave a Comment

Recent Posts

how to stop a docker container
How to Run a Docker Container
How to Push a Docker Image to a Private Repository
How to push a docker image
How to Tag a Docker Image
Software Spice
Share via
Copy link
Powered by Social Snap