Open In App

Docker – Using Image Tags

Last Updated : 05 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Pre-requisite: Docker

Image tags are used to describe an image using simple labels and aliases. Tags can be the version of the project, features of the Image, or simply your name, pretty much anything that can describe the Image. It helps you manage the project’s version and lets you keep track of the overall development process. 

Understanding The Docker Tags

Docker tags are labels for container images, used to differentiate versions and variants of an image during development and deployment. Docker tags will help you identify the various versions of docker images and help distinguish between them. Docker image will help us to build continuous deployment very quickly

Build An Image Using Docker Tag

You can specify a tag to the image right when you are building it using the -t flag. If you don’t specify a tag, it is automatically tagged with the latest tag.

sudo docker build -t <image-name>:<tag-name>

You can also specify the Image tag you want to pull in the Dockerfile. Let’s say you have a Dockerfile to pull an Ubuntu Image with the latest version.

FROM ubuntu:latest

If you want to build the Image with a custom tag called my-ubuntu, you can use the following command.

sudo docker build -t tag-demo:my-ubuntu .
Docker Image Tag

 

Tag An Image Using The Docker Tag Command

Docker images can be tagged by using Docker CLI as shown below.

docker tag Source_Image[:Tag] Target_Image[:Tag]

In the place of SOURCE_IMAGE mention the image that you want to tag and TARGET_IMAGE with the desired name for the tagged image. To differentiate versions, you can also optionally specify a TAG for both the source and target images.

Example:

if you have an image named “gfg_app” and you want to tag it as version 2, you can use the command:

docker tag gfg_app gfg_app:2

This will create a new image with the tag “2” based on the original “gfg_app” image.

Note: Ensure you have the necessary permissions and access to the Docker daemon to perform tagging operations.

Build An Image With a Single Docker Tag

While building the image by using Dockerfile also you can tag the image by using the following command.

docker build -t gfg/gfg_app: 1

“-t” represents the option called tag while “gfg” represents the name of the repository and “gfg_app” represents the name of the image with tag 1.

Build An Image With Multiple Docker Tags

Docker can assign multiple tags to a single image. This can be done while building the image as shown below.

docker build -t gfg/gfg_app: 1 -t gfg/gfg_webapp: 1

Here we are using the “-t” command two times for tagging the image with two different names.

Build An Image Without Any Tag

Building the image with only a tag is always not an option. You can build the image without using a tag also the main purpose of the tag is to keep the progress of the images you are building.

docker build -t gfg/gfg_app.

Tag An Image Using Docker Tag Command

You can also tag an Image directly using the tag sub-command.

sudo docker tag <imageId> <imageName>/<tagName>
Docker tag

 

You can see that the new tag has been assigned to the Image.

Use Of Tag In Docker Pull Command

You can pull a Docker Image using the pull sub-command. You can specify the tag of the Image that you want to pull. Note that if you don’t specify a tag, it will automatically pull the latest version of the Image by appending the “latest” tag.

sudo docker pull alpine:3.6
Docker Pull

 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads