Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we'll learn the concept of tags in Docker.

Docker provides the support for storing the images on the Docker Hub repository. A Docker tag provides a unique identity to a Docker image. There are sets of similar images with different versions identified by tags in a Docker repository.

Here, we'll learn to tag an image using the docker build and docker tag command.

2. Understanding the Docker Tags

The Docker tag helps maintain the build version to push the image to the Docker Hub. The Docker Hub allows us to group images together based on name and tag. Multiple Docker tags can point to a particular image. Basically, As in Git, Docker tags are similar to a specific commit. Docker tags are just an alias for an image ID.

The tag's name must be an ASCII character string and may include lowercase and uppercase letters, digits, underscores, periods, and dashes. In addition, the tag names must not begin with a period or a dash, and they can only contain 128 characters.

3. Build an Image Using Docker Tag

Before we move forward, let's first create a sample Dockerfile to demonstrate the tagging:

FROM centos:7
RUN yum -y install wget 
RUN yum -y install unzip 
RUN yum -y install java-1.8.0-openjdk 
RUN yum clean all
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
RUN export JAVA_HOME

In the above Dockerfile, we run all the necessary commands to install java using “centos:7” as a base image.

3.1. Build an Image with Single Docker Tag

In Docker, we can tag an image during the build time. To illustrate, let's check out the command to tag an image:

$ docker build -t baeldung-java:5 .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM centos:7
 ---> eeb6ee3f44bd
Step 2/2 : RUN yum -y install wget
 ---> Using cache
 ---> 46ee47a7422d
Successfully built 46ee47a7422d
Successfully tagged baeldung-java:5

Here, in the above command, we provided the “baeldung-java:5” as a tag to the Docker image. The tag in Docker is useful to maintain the version of the build to push the image to the DockerHub. The versioning is generally used to deploy any Docker image or get back to the older version.

We can also provide the tag with username and image name using the below syntax:

$ docker build -t baeldung/baeldung-java:5 .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM centos:7
 ---> eeb6ee3f44bd
....
Successfully built 46ee47a7422d
Successfully tagged baeldung/baeldung-java:5

Here, in the above command, we have provided the username “baeldung” with the image name “baeldung-java” and the tag as “5“.

3.2. Build an Image with Multiple Docker Tags

In Docker, we can also assign multiple tags to an image. Here, we'll use the docker build command to assign multiple tags to an image in a single command.

To demonstrate, let's check out the command for the above Dockerfile:

$ docker build -t baeldung-java:5 -t baeldung-java:6 .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM centos:7
 ---> eeb6ee3f44bd
....
Successfully built 46ee47a7422d
Successfully tagged baeldung-java:5
Successfully tagged baeldung-java:6

Here, we can see that 2 tags “baeldung-java:5” and “baeldung-java:6” are created for imageId46ee47a7422d“.

3.3. Build an Image Without Any Tag

We can also build a Docker image without using any tag. But in order to keep track of the image, we should always provide a tag with the image name. Let's look into the command to build an image without the tag:

$ docker build -t baeldung-java .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM centos:7
 ---> eeb6ee3f44bd
...
Successfully built 46ee47a7422d
Successfully tagged baeldung-java:latest

Here, in the above command, we built the image without any tag, so By default, Docker provides a tag to the image as the latest “baeldung-java:latest”.

Docker always points to the latest stable release using the latest tag. Old releases can even be called the latest. But we can't predict whether it is a major or minor version.

4. Tag an Image Using docker tag Command

So far, we have discussed tagging an image using the docker build command. But we can also explicitly tag an image using the docker tag command. Tagging an image just creates an alias to an image name or an imageId. Here, we'll explore both the ways to tag an image.

The general format of a Docker image name is as follows:

<user-name>/<image-name>:<tag-name>

In the above snippet, the component after the colon indicates the tag attached to the image.

Let's look into the command to tag an image using the image name:

$ docker tag baeldung-java:6 baeldung-java:8 

The command to tag an image using an imageId is as follows:

$ docker tag 46ee47a7422d baeldung-java:9 

Let's check out all the images created so far:

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
baeldung-java       5                   46ee47a7422d        13 minutes ago      370MB
baeldung-java       6                   46ee47a7422d        13 minutes ago      370MB
baeldung-java       8                   46ee47a7422d        13 minutes ago      370MB
baeldung-java       9                   46ee47a7422d        13 minutes ago      370MB
baeldung-java       latest              46ee47a7422d        13 minutes ago      370MB
centos              7                   eeb6ee3f44bd        7 months ago        204MB

Here, we'll find all the images created so far.

5. Use of Tag in docker pull Command

The Docker tags are useful in creating an image or pulling an image from a Docker Hub repository.
In our Dockerfile, we used the command FROM centos:7. This will pull version “7″ of the centos public image.

We can also pull an image with or without a tag.

Let's look into a command with a specific tag:

$ docker pull centos:7

docker pull command without any tag:

$ docker pull centos

The above command will pull the “centos:latest” image from the public Docker Hub repository. We can also apply multiple tags to an image, usually to specify major and minor versions.

6. Conclusion

In this article, we learned to create and manage the tags in Docker. We explored various ways to tag an image.

Using the docker build command, we first tagged an image. Later, we looked into the docker tag command. In addition, we explored the docker pull command using tags.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!