Docker Tag
1. Introduction
In this article, we will explain Docker Tag with examples. A Docker tag is a named reference to an image. It is like a branch reference in Git. This tag can be used to pull and run images. The updates to the images can be pushed automatically like in Git.
2. Docker Tag
Docker is used in DevOps as a software package to create containers for deployment. It helps in handling changes in code, requirements, operating systems, and cloud environments. Tags help in identifying the images and the derived images. You can go to Docker Hub and choose a tag to pull the image. The latest version will be pushed to the Docker Hub by the authors.
2.1 Prerequisites
Docker
software is required. Apache Tomcat image is obtained from Docker Hub
.
2.2 Download
You can download Docker
from the site.
2.3 Setup for Docker
Below is the setup check commands after installing the docker package
.
You can check the docker installation by running the hello-world docker repo which is located at the docker hub.
Docker Hello-World
docker run hello-world
The output of the above command will be as below:
Output
Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
2.4 Docker
You can create your own Repo and images. Dockerfile details are shown below: Dockerfile
The command to create docker
is shown below:
Docker File
FROM busybox CMD echo "Greetings! Docker image is created."
The output of the docker build command will be as below:
Docker Repo creation Output
docker build -t bhagvanarch/docker-repo . Sending build context to Docker daemon 2.048kB Step 1/2 : FROM busybox latest: Pulling from library/busybox 76df9210b28c: Pull complete Digest: sha256:95cf004f559831017cdf4628aaf1bb30133677be8702a8c5f2994629f637a209 Status: Downloaded newer image for busybox:latest ---> 1c35c4412082 Step 2/2 : CMD echo "Greetings! Docker image is created." ---> Running in bc9f0d8ac3d7 Removing intermediate container bc9f0d8ac3d7 ---> 166dae6f16f8 Successfully built 166dae6f16f8 Successfully tagged bhagvanarch/docker-repo:latest
2.5 Building Images with Docker Tags
2.5.1 Single Docker Tag
The tag identifies the build version to push the image to the Hub. The Hub allows us to group a set of images based on name and tag. The tag must be an ASCII character string and may include lowercase and uppercase letters, digits, underscores, periods, and dashes. The tag names should not begin with a period or a dash, and they can only contain 128 characters. The command is shown below:
Docker Repo – Single Tag
docker build -t bhagvanarch/docker-repo:1 .
2.5.2 Multiple Docker Tags
Multiple tags can be there for a particular image. Like in Git, tags are similar to a specific commit. They are image ID aliases. The command is shown below:
Docker Repo – Multiple Tags – Command
docker build -t bhagvanarch/docker-repo:1 -t bhagvanarch/docker-repo:2 .
2.6 Build an Image Without Any Tag
You can also build an image without using any tag. You should always have a tag with the image name to check the image. The command is shown below:
Docker Repo – No Tag – Command
docker build -t bhagvanarch/docker-repo .
2.7 Using docker tag Command
You 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. Below are the commands for both.
Docker Tag – Command
docker tag bhagvanarch/docker-repo:1 bhagvanarch/docker-repo:2 docker tag 23422daf2 bhagvanarch/docker-repo:2
2.8 Use of Tag in docker pull Command
You can pull the image from the Hub using the tags. You can also pull an image with or without a tag. The commands are shown below:
Docker Pull – Command
docker pull bhagvanarch/docker-repo:2 docker pull bhagvanarch/docker-repo
3. Download the Source Code
Download
You can download the full source code of this example here: Docker Tag