How to Use Docker Tags

29/12/2020
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.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Optimizing Docker Images

Developers like working with Docker for its flexibility and ease-of-use. When creating applications, it’s worth investing...
28/12/2020

How to List Docker Containers

Docker as you all know is a great containerization software. With Docker, you can create lightweight containers and run...
29/12/2020

Docker Compose — Memory Limits

Docker compose is a powerful utility. It saves time and reduces errors when deploying your Dockerized application. Usually,...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024