Tag an image using its ID
In this step, we will learn how to tag a Docker image using its Image ID. Tagging an image allows you to give it a new name and/or tag, creating a new reference that points to the same image content. This is useful for creating aliases, versioning, or preparing an image to be pushed to a different registry.
The basic command to tag an image is docker tag
. The syntax is:
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
You can specify the source image using its Image ID, its name, or its name and tag. In this step, we will use the Image ID.
First, let's list the images again to get the Image ID of the hello-world
image we pulled in the previous step.
docker images
Find the hello-world
image in the output and note its IMAGE ID
. It will be a string of hexadecimal characters, for example, bf756fb1cdb1
. You only need to use the first few characters of the ID, as long as they are unique among your images.
Now, let's tag the hello-world
image using its Image ID. We will tag it with a new name, my-hello-world
, and a tag v1.0
. Replace <image_id>
with the actual Image ID you noted from the docker images
output.
docker tag < image_id > my-hello-world:v1.0
There will be no output if the command is successful.
Now, let's list the images again to see the new tag.
docker images
You should now see a new entry with the repository my-hello-world
and tag v1.0
. Notice that it has the same IMAGE ID
as the original hello-world
image. This confirms that the new tag is just a pointer to the same image content.
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest <image_id> <created_time> <size>
my-hello-world v1.0 <image_id> <created_time> <size>
You have successfully tagged an image using its Image ID. This is a fundamental operation in managing Docker images.