Docker

Docker – Is it Possible for Image to Have Multiple Tags?

Docker images are the basic part of the Docker environment that are utilized to build and deploy projects within containers. The Docker developer often works with Docker images to containerize their projects, but occasionally they need to update the dependencies of an application or project. They also want to keep the changes from the previous version. For this purpose, the user can tag the image or specify the version of the image for unique identification.

This article will explain:

Is It Possible to Have Multiple Tags for an Image?

Yes, it is possible for a Docker image to have multiple tags. As in Docker, images are updated from time to time according to requirements, just like application updates. For this purpose, users need to specify the specific version of the image or a unique tag for identification. Therefore, the Docker image can have multiple copies of an image along with unique tags.

How to Build an Image With Various Tags?

You can build the same image with multiple tags while building the image from a Dockerfile. For this purpose, go through the given instructions.

Step 1: Make Dockerfile
First, make a simple file named “Dockerfile”. Then, paste the below-coded instruction into Dockerfile:

FROM python
WORKDIR /src/app
COPY . .
CMD [ "python", "./pythonapp.py" ]

The above instructions build an image to execute a simple python program that is defined in the “pythonapp.py” file:

Step 2: Built an Image With Multiple Tags
Next, utilize the “docker build” to generate the image with multiple tags. Users can utilize the “-t” or “–tag” option to build multiple tag images. For instance, to create an image with three different tags, we have to use the “-t” option three times:

> docker build -t python:latest -t python:3.6 -t python:3.4

Step 3: Verification
Now, verify if the image has been created with the specified tags:

> docker images

Tag Images in Docker

However, users can tag an image multiple times. In other words, you can specify the different tags for a single or similar image. For this purpose, the “docker tag” can be utilized.

Follow up on the below-mentioned procedure to tag the image in Docker.

Step 1: Tag the Image
To tag the image to specify the version of the image, utilize the “docker tag <original-image-name> <target-image-name>:<tag>” command:

> docker tag python:latest python:2.4

In the above command, we have tagged the “python:latest” image as “python:2.4”:

You can tag a single image many times to specify the multiple tags of an image. For instance, we have again tagged the “python:latest” image as “python:2.8”:

> docker tag python:latest python:2.8

Step 2: Verify If the Image is Tagged
Next, check out all images in Docker to verify it the newly tagged images are created or not:

> docker images

It can be observed that we have defined the two tags for “python:latest” image.

Conclusion

Yes! It is possible for an image to have multiple tags. As the images are updated from time to time and users must be required to assign unique identifying tags to an image. However, you can build the same image with multiple tags using the “docker build -t <image-name> -t <image-name>:tag” command. To create an image of multiple tags, use the “-t” option multiple times. This blog has illustrated that it is possible for an image to have different tags.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.