We can use the Docker tag command to add metadata to Docker images. They convey essential information about the version of a specific image. Docker registries such as Docker hub store images in repositories. A repository is a set of similar images but different versions identified using tags. For example, the Ubuntu repository in the Docker hub has several Ubuntu images, but all of them have different tags such as 18.04, focal, xenial, bionic, etc.
Docker tag is just a way to refer to a particular version of an image. A fair analogy is how we use Git tags to refer to specific commits in history. We can use Docker tags to provide specific labels to an image. They can be considered an alias to image IDs.
In this article, we are going to understand how to use the Docker tag command to tag Docker images. Before we move ahead, it’s recommended that you understand the following topics before jumping into Docker tags.
- What are Docker Images?
- How to Build Docker Images?
- Docker Push | How to Push Docker Images to Dockerhub?
Docker Tag Command
We can use the Docker tag command to tag an existing Docker image in our host system. The image names are usually of the form –
Here, the component after the colon specifies the tag given to the image.
We can only specify tag names that contain valid ASCII characters only. However, they can contain digits, periods, underscores, dashes, and both lowercase and uppercase letters. But we need to ensure that a tag name does not begin with a dash or a period. They can only have a maximum of 128 chars.
Docker tags allow us to group our images together by creating a unique combination of name:tag. We can then upload these unique images to share them on the Docker hub.
Image tags are optional. We don’t have to provide tags to images, however, it’s recommended to specify a tag to keep track of the purpose of the image and its specific version. Moreover, specifying image tags becomes essential when we want to push the images to any repository.
If we don’t provide any tag to the image while building it, Docker automatically specifies a tag called latest to the image. This specifies that the image is of the latest version in its repository.
The syntax of the Docker tag command is –
$ docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Using the above command, we can specify a new tag to a source image that and create a target image. Let’s try to tag an image using this command. First, let’s list all the existing images in our machine.
$ docker images
We can now use the Docker tag command to tag the ubuntu:latest image with a new tag. Let’s try to give this image a tag called myubuntu.
$ docker tag ubuntu:latest ubuntu:myubuntu
Use-Cases of Docker Tags
There are 3 scenarios where we use Docker tags. Let’s discuss them one by one.
FROM instruction inside a Dockerfile
When we use the FROM instruction inside a Dockerfile to pull base images from Dockerhub, we specify the tag of the image as well. For example, if we want to pull the image specific to Ubuntu 20.04 version, we can use the instruction below.
Here, the component after colon specifies the tag of the Ubuntu image that we want to pull. If we don’t specify any tag, the Docker assumes that the user wants to pull the latest image from Dockerhub. It automatically embeds the latest tag with the image.
Building an Image
When we use the Docker build command to build an image from a Dockerfile, we use the -t option to give a name and tag to the image. For example, let’s check out the Dockerfile and the command below.
FROM ubuntu:focal WORKDIR /app
Docker Build –
$ docker build -t myubuntuimage:version1 .
Here, we have given our image a name called myubuntuimage and a tag called version1. The . (dot) here specifies the location of the Dockerfile. Let’s try to build this image and list all the images to verify the tag.
You can see that our new tagged image has been created.
Docker Tags to Explicitly Tag an Image
As already discussed above, we can use the Docker tag command to explicitly tag Docker images. Let’s check out the following scenarios.
Tag Docker Images Referenced by ID
If you have the ID of an existing image, you can use it to give it a new tag.
$ docker tag 15d10754d6d0 myubuntuimage:version2
Here, we have used the image ID of an already existing image to give that image, a new tag.
Tag Image for a Private Registry
Docker allows you to create your own private registry in your local machine. You can use the following steps to create a private registry.
- Pull the registry image from the Docker hub.
$ docker pull registry:latest
- Create a container associated with the registry image.
$ docker run -d -p 5000:5000 --name registry registry:latest
- Keep the container running, and tag an already existing image that you want to push as follows –
$ docker tag <source-image-name>:<source-tag-name> localhost:5000/<newimage>
- Now, push your image to the registry that you just created.
$ docker push localhost:5000/<newimage>
- Your image has now been pushed to the new private registry. You can commit the registry container to save the changes. To pull the image back, you can use the following command.
$ docker pull localhost:5000/ubuntu
What happens when we don’t specify image tags?
The default tag given to an image is the latest tag. This comes into the picture when we don’t specify any tags to the image. Docker assumes that the image that we are creating is the latest version of the images in the repository and hence, it automatically embeds the latest tag to it. Even when building an image using the Docker build command, if we don’t specify any tag it assigns the latest tag to it.
When pulling images from Dockerhub, you might have noticed that if you don’t specify the tag of the image that you want to pull, it automatically starts pulling the latest one.
Docker tags help us to give a label or metadata to the images so that we can easily identify the version or purpose of the image. We can pretty much provide any legal name as a tag to the image. It can be the creator’s name, version of the image, use of the image, name of the application, etc. Anything which you think will help you to uniquely identify the image later on.
In case you have any queries or suggestions, please mention them in the comment box and we will have our experts get back to you. Also, please check out our free Docker Tutorials.