The docker tag command is used to tag or label the image, set the Docker image version, and make it usable for others. To tag the Docker image, the user can either use the “-t” option while building the image or use the “docker tag <source image>:<version> <target image>:<version>” command.

What is Docker Tag Command?

The “docker tag” or “docker image tag” is a Docker CLI command that creates a new tagged target image associated with a source image. This command labels the Docker image and sets the image version, which helps manage and track the image. This feature also helps the developer roll out and roll back changes, enhance traceability, and troubleshoot for larger applications.

How to Create and Tag Docker Image?

While developing or deploying the application in Docker, the user is usually required to update or upgrade the image. In this situation, it is necessary to label the image with a unique tag or version for identification. For this purpose, the user can use the “-t” option while generating the image or the “docker tag” command.

For the proper illustrations, go through the below instructions.

Step 1: Make Dockerfile

Create a file named “Dockerfile” in the code editor. For illustration, we have used the “nano” editor:

sudo nano Dockerfile
sudo nano Dockerfile

Then, add the below-mentioned Dockerfile commands into the file:    

FROM nginx:latest
COPY index.html /usr/share/nginx/html/index.html
EXPOSE 80
ENTRYPOINT ["nginx", "-g", "daemon off;"]

In the given code snippet: 

  • The “FROM” instruction defines the parent image for the container.
  •  The “COPY” command adds the code or program file or directory to the container’s specified path.
  • The “EXPOSE” allocates the port where the application will be published inside the container.
  •  The “ENTRYPOINT” command sets the point from where the execution will be started.

Step 2: Generate Program

In the next step, generate an HTML file named “index.html” that contains simple HTML webpage code:

sudo nano index.html

For practice, you can use the following HTML:

!DOCTYPE html>
<html>
<head>
  <title>Sample Application</title>
  <style>
  body {
        background-color: #D2B48C;
        }
    .container {
        text-align: center;
        padding: 50px;
        }
    p {
    color:#800000;
    font-weight: bold;
      }
  </style>
</head>
<body>
<div class="container">
  <h1>Welcome to Hands-on Cloud Tutorial</h1>
  <p> Docker Tag Command Beginner Guide</p>
  </div>
</body>
</html>

Step 3: Built and Tag Image

Next, generate the new container image from the Docker file through the below command:

docker build -t demo-image .

In the above command, the “-t” option will set the tag for container image. If the Docker image version or tag is not defined, by default, it will tagged as the “Latest” image:

docker build -t demo-image .

For verification, list down the Docker images and check if the image is created and tagged or not:

docker images

The result shows that we have effectively tagged the “demo-image” as “latest”:

docker images

Step 4: Run Image

To execute the Docker image to containerize the program, utilize the below command:

docker run -p 80:80 --name demo-cont demo-image

Here, “-p” publishes the application on the host port, and the “–name” option allocates the name to the container:

docker run -p 80:80 --name demo-cont demo-image

Navigate to the Firefox Linux browser, open the http://localhost, and check if the application is accessible or not:

Output

How to Tag Docker Image Using “docker tag” Command?

To set the version of the image or label the image later, the user can use the following command:

docker tag demo-image demo-image:1.0

In the above command, we have tagged the “demo-image” as “demo-image:1.0”. Here, “1.0” is a tag or version of the “demo-image” image:

docker tag demo-image demo-image:1.0

For verification, view all the Docker snapshots using the below command:

docker images

From the below-pointed image, you can see we have effectively tagged the image as the “1.0”:

docker images

How to Tag Image to Push it to Docker Repository?

The “docker tag” command lets us maintain the image build versions. It also permits us to group and publish the Docker images on Docker Hub based on image names and tags. To tag the container snapshot to push it to the Docker Hub, go through the following demonstration:

 Step 1: Tag the Container Image

First tag the container snapshot using “docker tag <Source Image>:<Version> <Docker Hub Username>/<Target Image>:<Version>” command:

docker tag demo-image dockeruser019/demo-image:1.0

In the above command, the source image is the snapshot that you want to push, the Docker Hub username is required to define the path or account on which the image will be published, and the target image is an association of the source image that is going to be published on Docker Hub repository:

docker tag demo-image dockeruser019/demo-image:1.0

To check if the target image is created and tagged or not, list down the Docker images:

docker images

The given output shows that we have successfully tagged the “dockeruser019/demo-image:1.0” image:

In a similar way, you can tag the image to publish it or push it to a private or user-personal repository.

Step 2: Log In to Docker Hub

To publish the image on Docker Hub, log in to the Docker Hub account through the given command:

docker login

Type in the Username and Password of the account and hit Enter:

Step 3: Push the Image

Next, publish the container blueprint (image) in the repository of the Docker hub through the mentioned command:

docker push dockeruser019/demo-image:1.0

Step 4: Verification

For confirmation, navigate to the Docker Hub repository from the browser, under the “Repositories”, and check if the tagged image is published or not:

Docker Repository

The below result shows that we have effectively tagged and published the image on Docker Hub:

That is it for the “docker tag” command.

Conclusion

The “docker tag” command maintains the image build version and helps us to manage and publish the image on the Docker Hub official or private user repository. To tag the Docker image, the user can either use the “-t” option while creating the new image through “docker build” or tag the image later through “docker tag <source image>:<version> <target image>:<version>” command. To tag the image to push it to the official repository, you can use the “docker tag <Source Image>:<Version> <Docker Hub Username>/<Target Image>:<Version>” command. This write-up has explained the “docker tag” command with practical demonstrations.