1. Overview
1.概述
Docker is a very widely adopted containerization technology. A variety of applications can be run in containers.
Docker是一种非常广泛采用的容器化技术。各种各样的应用程序都可以在容器中运行。
While we can control the name of a container when we launch it, the ID is generated by Docker. We may need this ID to perform certain operations on the Docker host, so finding a container’s ID from its name is a very common requirement.
虽然我们可以在启动容器时控制它的名字,但ID是由Docker生成的。我们可能需要这个ID来对Docker主机进行某些操作,所以从容器的名称中找到它的ID是一个非常常见的要求。
In this short tutorial, we’ll discuss various ways to find the container ID from its name.
在这个简短的教程中,我们将讨论从容器名称中找到容器ID的各种方法。
2. Setting up an Example
2.设置一个例子
Let’s create a few containers to use as an example:
让我们创建几个容器,作为一个例子。
$ docker container run --rm --name web-server-1 -d nginx:alpine
$ docker container run --rm --name web-server-10 -d nginx:alpine
$ docker container run --rm --name web-server-11 -d nginx:alpine
Now, let’s check that these containers have been created:
现在,让我们检查一下这些容器是否已经被创建。
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80f1bc1e7feb nginx:alpine "/docker-entrypoint.…" 36 seconds ago Up 36 seconds 80/tcp web-server-11
acdea168264a nginx:alpine "/docker-entrypoint.…" 36 seconds ago Up 36 seconds 80/tcp web-server-10
0cbfc6c17009 nginx:alpine "/docker-entrypoint.…" 37 seconds ago Up 36 seconds 80/tcp web-server-1
As we can see, we have three containers in the running state using the nginx image.
正如我们所看到的,我们有三个使用nginx镜像的容器处于运行状态。
3. Displaying the Short Container ID
3.显示简短的集装箱ID
Docker assigns a unique ID to each container. The full container ID is a hexadecimal string of 64 characters. However, in most cases, the short version of this container ID is sufficient. The short container ID represents the first 12 characters of the full container ID.
Docker为每个容器分配了一个唯一的ID。完整的容器ID是一个64个字符的十六进制字符串。然而,在大多数情况下,这个容器ID的短版本就足够了。短的容器ID代表完整容器ID的前12个字符。
Let’s display the short container ID using Docker’s container ls child command:
让我们使用Docker的container ls子命令来显示简短的容器ID。
$ docker container ls --all --quiet --filter "name=web-server-10"
acdea168264a
In this example, we’ve used the –filter option, which filters the output based on a condition. In our case, filtering is done on the container’s name.
在这个例子中,我们使用了-filter选项,它根据一个条件来过滤输出。在我们的例子中,过滤是根据容器的名称进行的。
Additionally, we also used –all and –quiet options with the command. The –all option is required to show all containers because, by default, it only shows the running containers. The –quiet option is used to show the container ID only.
此外,我们还在命令中使用了-all和-quiet选项。-all选项是需要显示所有容器的,因为在默认情况下,它只显示运行中的容器。-quiet选项用于只显示容器ID。
We can also use the combination of grep and awk commands to display the short container ID:
$ docker container ls --all | grep web-server-10 | awk '{print $1}'
acdea168264a
Here, the awk command prints the first column of the output, which represents the short container ID.
这里,awk命令打印了输出的第一列,它代表了短的容器ID。
We should note that the grep and awk commands may not be available on all platforms. Therefore this approach is less portable.
我们应该注意,grep和awk命令可能不是在所有平台上都可用。因此,这种方法的可移植性较差。
4. Displaying the Full Container ID
4.显示完整的集装箱ID
In most cases, a short container ID will suffice. However, in rare scenarios, the full container ID is needed to avoid ambiguity.
在大多数情况下,一个简短的容器ID就足够了。然而,在极少数情况下,需要完整的容器ID,以避免产生歧义。
We can use the Docker’s container ls child command to display the full container ID:
我们可以使用Docker的container ls子命令来显示完整的容器ID。
$ docker container ls --all --quiet --no-trunc --filter "name=web-server-10"
acdea168264a08f9aaca0dfc82ff3551418dfd22d02b713142a6843caa2f61bf
Here, we’ve used the –no-trunc option with the command. This option overrides the default behavior and disables the output truncation.
这里,我们在命令中使用了-no-trunc选项。这个选项覆盖了默认行为,并禁用了输出截断。
We can achieve the same result using the combination of the grep and awk commands:
我们可以使用grep和awk命令的组合实现同样的结果。
$ docker container ls --all --no-trunc | grep web-server-10 | awk '{print $1}'
acdea168264a08f9aaca0dfc82ff3551418dfd22d02b713142a6843caa2f61bf
Docker’s container inspect child command shows detailed information about the container in a JSON format. We can use it to display the container ID:
Docker的container inspect子命令以JSON格式显示关于容器的详细信息。我们可以用它来显示容器的ID。
$ docker container inspect web-server-10 --format={{.Id}}
acdea168264a08f9aaca0dfc82ff3551418dfd22d02b713142a6843caa2f61bf
In this example, we’ve used the –format option, which uses the Go template to extract the Id field from the JSON output.
在这个例子中,我们使用了-format选项,它使用Go模板从JSON输出中提取Id域。
5. Displaying the Container ID Using an Exact Match
5.使用精确匹配显示容器ID
We can’t use the basic grep or container ls child commands in all scenarios. For example, this naive approach will not work if container names are partially matching. Let’s see this with an example.
我们不能在所有情况下都使用基本的grep或container ls子命令。例如,如果容器名称是部分匹配的,这种天真的方法就不起作用。让我们通过一个例子来看看。
Let’s display the ID of the web-server-1 container:
让我们显示一下web-server-1容器的ID。
$ docker container ls --all --quiet --filter "name=web-server-1"
80f1bc1e7feb
acdea168264a
0cbfc6c17009
Here, the output shows three container IDs. This happens because the name web-server-1 partially matches the other two containers – web-server-10 and web-server-11. To avoid this, we can use regular expressions.
这里,输出显示了三个容器的ID。这是因为web-server-1这个名字与另外两个容器–web-server-10和web-server-11部分匹配。为了避免这种情况,我们可以使用正则表达式。
Now, let’s use the regular expression with the container name:
现在,让我们使用容器名称的正则表达式。
$ docker container ls --all --quiet --filter "name=^web-server-1$"
0cbfc6c17009
In this example, we’ve used the caret(^) and dollar($) symbols to enforce an exact match on the container’s name.
在这个例子中,我们使用了caret(^)和dollar($)符号来强制对容器的名称进行精确匹配。
In a similar way, we can use the -w option with the grep command to enforce the exact match:
以类似的方式,我们可以在grep命令中使用-w选项来强制执行精确匹配。
$ docker container ls --all | grep -w web-server-1 | awk '{print $1}'
0cbfc6c17009
6. Conclusion
6.结语
In this article, we saw how to find a container ID using its name.
在这篇文章中,我们看到了如何使用容器的名称找到一个容器的ID。
First, we used the container ls child command as well as the combination of grep and awk commands to display the short container ID.
首先,我们使用container ls子命令以及grep和awk命令的组合来显示短的容器ID。
Then we used the –no-trunc option and container inspect child command to display the full container ID.
然后我们使用-no-trunc选项和 container inspect子命令来显示完整的容器ID。
Finally, we used regular expressions to ensure an exact match of the container name.
最后,我们使用正则表达式来确保容器名称的精确匹配。