Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

Docker is a very widely adopted containerization technology. A variety of applications can be run in containers.

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.

In this short tutorial, we’ll discuss various ways to find the container ID from its name.

2. Setting up an Example

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.

3. Displaying the Short Container 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.  

Let’s display the short container ID using Docker’s container ls child command:

$ 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.

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.

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.

We should note that the grep and awk commands may not be available on all platforms. Therefore this approach is less portable.

4. Displaying the Full Container ID

In most cases, a short container ID will suffice. However, in rare scenarios, the full container ID is needed to avoid ambiguity.

We can use the Docker’s container ls child command to display the full container 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.

We can achieve the same result using the combination of the grep and awk commands:

$ 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 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.

5. Displaying the Container ID Using an Exact Match

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.

Let’s display the ID of the web-server-1 container:

$ 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.

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.

In a similar way, we can use the -w option with the grep command to enforce the exact match:

$ docker container ls --all | grep -w web-server-1 | awk '{print $1}'
0cbfc6c17009

6. Conclusion

In this article, we saw how to find a container ID using its name.

First, we used the container ls child command as well as the combination of grep and awk commands to display the short container ID.

Then we used the –no-trunc option and container inspect child command to display the full container ID.

Finally, we used regular expressions to ensure an exact match of the container name.

Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!