How to tag Docker images?

    If you have been working with Docker containers and images for quite a while now, you might have tons of images already in your local machine. In fact, some of these Docker images might even be from the same repository. For example, you might have fedora images with different versions such as 24, 26, latest, etc. Managing such a large number of images and remember which image or version of the image you were working with previously might be difficult sometimes.

    Docker Tags

    Docker comes to the rescue again. Docker allows you to add metadata to the images in the form of tags. Tags are just names assigned to images in Docker. It’s mandatory that all the images in Docker have a tag associated with them. When you specify a FROM instruction inside a Dockerfile, you might have noticed that syntax to be “FROM <image>:<tag>”. This is where tags come into play. Tags can be anything. They can be a version of the application environment that they represent, versions of the images, simply a keyword associated with the image, name of the maintainer, intended destination such as a stage or pod, stability, etc. Anything which helps you to remember the specifics of the image can be a tag. If we don’t assign any tag to a Docker image that we create using Dockerfile, it defaults to the “latest” tag. We will talk about that later on in this tutorial. If we want to add a tag to an image that does not have one (“latest” by default) or has any other tag, we can use the Docker tag command as mentioned below.

    $ docker tag <source-image>[:tag] <repo-name>/<target-image>[:tag]

    Please note that a tag must be a valid string containing only ASCII characters which can be lowercase or uppercase alphabets, digits, dashes, periods, underscores, etc. Also, the size of a tag can only be up to 128 chars. And you are not allowed to start a tag name with a dash or a period. Let’s check out a few examples where we will demonstrate the different scenarios where we can use the Docker tags command.

    Examples

    1. Tagging Docker images referenced by image IDs

    Let’s try to tag a Docker image by specifying the ID of the image as the source and the target image name along with the new tag that we want it to give. First, let’s check which images do we have currently in our local machine.

    $ docker images

    $ docker images

    Let’s try to give a separate tag to the mysql/mysql-server image which currently has the latest tag.

    $ docker tag <image-id> mysql/mysql-server:mydatabase

    $ docker tag <image-id> mysql/mysql-server:mydatabase

    We can see that our image has been tagged and a new copy has been created.

    2. Tagging Docker images referenced by Image names

    Instead of tagging an image referenced by image ID, we can also specify the source image name to tag it to create a target image. Let’s see how to do so.

    $ docker tag mysql mysql:mydatabase

    $ docker tag mysql mysql:mydatabase

    You can see that the MySQL image has been newly tagged.

    3. Tagging Docker images referenced by both image name and tag

    If there are multiple images with the same name, you can even use both the name and tag to identify the source image and then tag it.

    $ docker tag python:latest python:myapp

    $ docker tag python:latest python:myapp

    4. Removing tags from Docker images

    We all know that we can remove Docker images using the docker rmi command. But if the images are tagged and we try to remove tagged images, then only the tagged images are removed and not the original ones. Let’s check it out.

    $ docker rmi python:myapp

    $ docker rmi python:myapp

    We can see that only the tagged image has been removed or untagged and the original ones remained intact.

    5. Re-tagging Docker images

    There is no specific command that Docker provides to re-tag images. You can simply provide another tag to the image and then remove the original one. Let’s check it out.

    $ docker tag mysql:mydatabase mysql:myDBVersion2
    $ docker rmi mysql:mydatabase

    $ docker rmi mysql:mydatabase

    The latest tag

    By default, if we do not provide any tag while pulling images from Dockerhub or creating our own customized image, then Docker automatically tags it to “latest” tag. This means that the pulled or created image will be of the latest version. For example, if we do not specify the version of the centOs image that we want to pull, it will pull the latest image.

    $ docker pull centos

    $ docker pull centos

    You can see that by default, it pulls the latest tag.

    Wrapping Up!

    To sum up, in this comprehensive tutorial, we discussed how the concept of tags works in Docker and what they represent. We discussed the general syntax of the Docker tag command which helps us to tag Docker images. We discussed different scenarios where we tagged Docker images referenced by image names, Ids, and even both of them. Finally, we also discussed how to remove tags from Docker images and re-tag them. We ended our discussion by looking at the default tag in Docker called “latest”. We hope that this tutorial gives you in-depth insights on tags in Docker and you will now be able to get hands-on with Docker tags. Happy Learning!

    People are also reading: