1. Overview
1.概述
In this tutorial, we’ll learn the concept of tags in Docker.
在本教程中,我们将学习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.
Docker提供了在Docker Hub仓库上存储镜像的支持。Docker标签为Docker镜像提供了一个独特的身份。在Docker资源库中,有一组由标签标识的具有不同版本的类似镜像。
Here, we’ll learn to tag an image using the docker build and docker tag command.
在这里,我们将学习使用docker build和docker tag命令来标记一个图像。
2. Understanding the Docker Tags
2.了解Docker标签
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.
Docker标签有助于维护构建版本,将镜像推送到Docker Hub。Docker Hub允许我们根据名称和标签将图像分组。多个Docker标签可以指向一个特定的图像。基本上,正如在Git中,Docker标签类似于一个特定的提交。Docker标签只是一个图像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.
标签的名称必须是一个ASCII字符串,可以包括小写和大写字母、数字、下划线、句号和破折号。此外,标签名称不能以句号或破折号开始,而且只能包含128个字符。
3. Build an Image Using Docker Tag
3.使用Docker标签建立一个镜像
Before we move forward, let’s first create a sample Dockerfile to demonstrate the tagging:
在我们继续前进之前,让我们先创建一个样本Docker文件来演示标记。
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.
在上面的Docker文件中,我们运行所有必要的命令来安装java,使用“centos:7”作为基础镜像。
3.1. Build an Image with Single Docker Tag
3.1.用单个Docker标签建立一个镜像
In Docker, we can tag an image during the build time. To illustrate, let’s check out the command to tag an image:
在Docker中,我们可以在构建过程中标记一个图像。为了说明这一点,我们来看看标记图像的命令。
$ 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.
在上述命令中,我们提供了”baeldung-java:5“作为Docker镜像的标签。Docker中的标签有助于维护构建的版本,以便将镜像推送到DockerHub。版本管理通常用于部署任何Docker镜像或返回到旧版本。
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“.
在这里,在上述命令中,我们提供了用户名“baeldung” ,图像名称为”baeldung-java“,标签为”5“。
3.2. Build an Image with Multiple Docker Tags
3.2.用多个Docker标签构建一个镜像
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.
在Docker中,我们也可以为一个镜像分配多个标签。这里,我们将使用docker build命令,在一条命令中为一个图像分配多个标签。
To demonstrate, let’s check out the command for the above Dockerfile:
为了演示,让我们看看上述Docker文件的命令。
$ 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 imageId “46ee47a7422d“.
在这里,我们可以看到2个标签“baeldung-java:5”和“baeldung-java:6”被创建为imageId“46ee47a7422d。
3.3. Build an Image Without Any Tag
3.3.建立一个没有任何标签的图像
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镜像。但为了跟踪镜像,我们应该始终提供一个带有镜像名称的标签。让我们来看看在没有标签的情况下构建镜像的命令。
$ 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为镜像提供了一个最新的”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.
Docker总是使用最新标签指向最新的稳定版本。旧版本甚至可以被称为最新版本。但我们无法预测它是大版本还是小版本。
4. Tag an Image Using docker tag Command
4.使用docker tag命令给图像打标签
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.
到目前为止,我们已经讨论了使用docker build命令来标记图片。但我们也可以使用docker tag命令明确地标记一个图像。标记一个图像只是创建一个图像名称或imageId的别名。在这里,我们将探讨标记图片的两种方式。
The general format of a Docker image name is as follows:
Docker镜像名称的一般格式如下。
<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:
使用imageId来标记图像的命令如下。
$ 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
5.在docker pull命令中使用标签
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.
Docker标签在创建镜像或从Docker Hub仓库中提取镜像时非常有用。
在我们的Docker文件中,我们使用了FROM centos:7.命令,这将拉出centos公共镜像的”7″版本。
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命令没有任何标签。
$ 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.
上述命令将从公共的Docker Hub仓库中提取“centos:latest”镜像。我们还可以对一个镜像应用多个标签,通常是为了指定主要和次要版本。
6. Conclusion
6.结语
In this article, we learned to create and manage the tags in Docker. We explored various ways to tag an image.
在这篇文章中,我们学习了如何在Docker中创建和管理标签。我们探索了对图像进行标记的各种方法。
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.
使用docker build命令,我们首先标记了一个图像。后来,我们研究了docker tag命令。此外,我们探索了使用标签的docker pull命令。