Docker images are an essential component of Docker containers. They are utilized to build and execute Docker containers. While working on Docker, developers may need to tag Docker images to uniquely identify and version their images. They help developers to assign meaningful names and versions to images so that they can be easily identified, managed, and shared with others. Moreover, developers can also push and pull images from the registry by tagging an image with the registry and repository name.
This article will explain various methods of tagging a Docker container image.
How to Tag Docker Container Image?
The Docker Container image can be tagged by different methods, such as:
Method 1: Tag a Docker Image by Name
To tag a Docker image referenced by name, the “docker tag <source-image-name> <docker hub-username>/<target-image-name>:<tag>” command is used.
Let us follow the below steps:
Step 1: Select Desired Docker Image
First, list all the Docker images using the provided command and choose a particular Docker image for tagging:
The above output displayed all the Docker images and we have selected the “img1” image.
Step 2: Tag Image Referenced by Name
To tag a Docker image referenced by name, use the “docker tag <source-image-name> <docker hub-username>/<target-image-name>:<tag>” command:
Step 3: Verify the Tagged Image
Now, verify whether the desired image has been tagged or not by listing all the Docker images:
It can be seen that we have successfully tagged the Docker image by name i.e., “laibayounas/img1” with the tag “v1.0”.
Method 2: Tag a Docker Image by ID
To tag a Docker image referenced by ID, the “docker tag <source-image-ID> <docker hub-username>/<target-image>:<tag>” command is utilized. Let us execute the below commands:
Step 1: Select a Particular Docker Image ID
First, display all Docker images and select a specific image ID:
In the above output, all the Docker images can be seen with their IDs. We have selected the “img2” image’s ID i.e., “513d5df01fda”.
Step 2: Tag the Image Referenced by the ID
Now, tag a Docker image by ID using the “docker tag <source-image-ID> <docker hub-username>/<target-image>:<tag>” command:
Step 3: Verification
Finally, display Docker images to ensure that the selected Docker image has been tagged successfully:
As you can see, ID has successfully tagged the Docker image i.e., “laibayounas/img2” with tag “v2.0”.
Method 3: Tag a Docker Image by Name and Tag
To tag a Docker image referenced by name and ID, utilize the “docker tag <source-image-name>:<source-tag> <docker hub-username>/<target-image>:<tag>” command.
Step 1: Select a Specific Docker Image with a Tag
First, list all Docker images and choose a specific Docker image with its tag:
The above output displayed all Docker images. We have selected the “aws-img” image and its tag “latest”.
Step 2: Tag Image Referenced by Name and Tag
To tag a Docker image referenced by name and tag, type out the “docker tag <source-image-name>:<source-tag> <docker hub-username>/<target-image>:<tag>” command:
Step 3: Verify the Tagged Image
Verify whether the Docker image has been tagged or not by displaying all Docker images:
The above output shows that the Docker image has been tagged successfully i.e., “laibayounas/aws-image” with tag “v3.0.latest”.
Conclusion
There are different methods to tag the Docker Container image, such as tagging a Docker image referenced by name or ID using the “docker tag <source-image-name/ID> <docker hub-username>/<target-image-name>:<tag>” command and by name and tag via the “docker tag <source-image-name>:<source-tag> <docker hub-username>/<target-image>:<tag>” command. This article has explained different methods of tagging a Docker container image.