Docker Image tags and how to use them

DockerOperating SystemOpen Source

Docker Image tags are simple labels or aliases given to a docker image before or after building an image to describe that particular image. It can be the version of the project or the container, features of the image, technologies used in the image or pretty much anything you want. It plays a key role in the overall software development life cycle because it helps you keep track of the different parts of your project as well as help you in version management of the product.

While pulling an image, you can specify the tag of the image you want or if not specified, it will pull the latest tagged image automatically. Let us see the two most common ways, where tagging comes into picture while you are working with docker images.

  • When you are trying to build an image using the docker build command, you can specify the tag along with the image name to build the image with that specific tag. You can use the −t flag to do so. Check out the command below to tag an image while building it.

sudo docker build −t <username>/<image−name>:<tag−name>.

What the above command does is, it searches for the dockerfile in the docker build context which you have set as the current directory by using the dot, using that dockerfile it builds the image and specifies the tag which you specified. However, specifying a tag name is not mandatory if you are using the above command. You can also build the image without specifying the tag.

  • You can also use the tag command to tag an already existing image. You can do so, by using the below command.

sudo docker tag <image−id> <image−name>/<tag−name>

The command stated above lets you tag an image using the image ID. You can find the image ID of a particular image using the docker images command.

If you don’t specify any tag while pulling an image, it will automatically pull the latest version of the image. See the examples below.

Inside the dockerfile, if you use −

FROM python

It will automatically embed a latest tag (python:latest) and will pull the latest python image from the docker registry.

However, if you write −

FROM python:3

It will pull python 3 from the docker registry.

Note that if you want to pull all the tagged versions associated with an image, you can use the −a flag along with the docker pull command

sudo docker pull ubuntu −a

This will pull all the available tagger versions of the image named ubuntu.

To conclude, tagging an image is very useful when you are simultaneously working with several images. It gives you better version control and management over your project. It lets you keep track of the major changes that you make in your project. It’s always a better practice to tag your image when you build it.

One last thing to discuss is about the tag called latest. When you don’t specify a tag to an image, by default docker puts the latest tag to your image so that when you try to pull the image back, by default it serves you with the image having the latest tag.

Published on 27-Oct-2020 12:16:27