Docker

How to Use Docker Tags

Docker is an open source containerization system. You can easily create containers of different operating systems and run some services or test your app. To create a Docker container, you need an image. Docker images are the base from which containers are created.

You can find massive amounts of Docker images if you go to Docker Hub – the official repository of Docker images at https://hub.docker.com/

The Docker images are uniquely identified by hash, more specifically SHA256 hash which is calculated depending on the contents of the image. The hash looks something like 24d15beb498b (short form) or 24d15beb498bb23d955af6f0f56acd0072f6bb1cecc3ebabef9c439c8e955771 (long form). Remembering these hashes are nearly impossible and pointless. Just like Git, you can easily name and tag your images and forget about all these hashes. This makes working with Docker images much easier.

Docker image identifier has 3 parts, username/image_name:tag. The username is the username of your Docker Hub account. If you don’t plan to host your Docker images in Docker Hub, you can leave the username part out. The image_name is what you want to call your image. The tag is of course, the tag of your Docker image. The username/image_name together is also called the repository of the image.

Now, let’s take a look at a scenario and then you will be able to understand why tags are needed.

Let’s say, you are creating a Docker image for the Apache HTTP server based on the Ubuntu image. Now, you can call it http-server of course. No problem. No tag needed right? Well, let’s say, you need to create the same Apache HTTP server image again, but you need a more lightweight solution, like Alpine. So, you want to create another Docker image for the Apache HTTP server based on Alpine Linux image. What are you going to call it? alpine-http-server? Well, you can of course do that. But you can do the same thing way better with tags.

For example, you could tag the Docker image which is based on Ubuntu like http-server:ubuntu, the Alpine one as http-server:alpine. Isn’t it easier to remember now? It also looks very clean.

Docker tag is a nice way to manage Docker images.

In this article, I will show you how to tag Docker images.  So, let’s get started.

Requirements:

You must have Docker installed on your computer if you want to try out the examples in this article.

If you don’t have Docker installed, then you may check out one of the articles below (depending on your operating system) to get Docker installed on your desired Linux distribution.

If you still have any problem installing Docker, you may contact me through https://support.linuxhint.com. I will be more than happy to help.

Tagging Images While Building Custom Images:

You can build custom Docker images using Dockerfile. When you build a custom Docker image from a Dockerfile, you can specify the repository and tag information of the Docker image being built.

Here, I have a simple Docker file. I am going to build a Docker image for my Node.js application using this Dockerfile.

First, you have to navigate to the directory where you saved the Dockerfile. In my case, it is the ~/Projects/docker/nodeapp directory.

$ cd ~/Projects/docker/nodeapp

As you can see, the Dockerfile is here.

Now, you can build a custom Docker image using the Dockerfile above and also tag the image with the following command:

$ docker build -t shovon8/nodeapp:v1.

NOTE: Here, shovon8 is the username, nodeapp is the image name and v1 is the tag name.

If you don’t plan to upload the custom built Docker image to Docker Hub, you can leave the username part out. Without the username part, the command to build the custom Docker image would be,

$ docker build -t nodeapp:v1.

You can also leave the tag part out if you want. In that case, the default tag latest will be used.

Without the username and tag part, the command would be,

$ docker build -t  nodeapp.

I am going to set the image name nodeapp and tag v1 for now.

The Docker image is successfully built and tagged as nodeapp:v1 as you can see.

When I list all the locally stored Docker images on my computer, the nodeapp:v1 image is listed there as well as you can see.

Tagging a Locally Stored Docker Image:

Let’s say, you have built a custom Docker image. Now, you want to tag it again. It may be because you thought you won’t be uploading this Docker image to Docker Hub, so you didn’t use your username while building the image. Don’t worry. You can add as many tags as you want to your locally stored Docker images.

Let’s add another tag to the image nodeapp:v1 that we just built in the earlier section of this article.

To add another tag to the Docker image nodeapp:v1, run the following command:

$ docker tag nodeapp:v1 shovon8/nodeapp:latest

Another tag should be added for the image nodeapp:v1. As you can see, both of these images nodeapp:v1 and shovon8/nodeapp:latest are of the same size (58.6 MB) and has the same hash 10d31b179449. So, they are basically the same image with different tag. They are like UNIX aliases.

Now that the custom built Docker image 10d31b179449 has the username, image name and tag, you can upload it to Docker Hub.

So, that’s how you use Docker tags. Thanks for reading this article.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.