Introduction to Docker List Containers
In order to list docker containers, we use the ‘docker container ls’ or ‘docker ps’ command. Both commands have the same flags as both commands operate on the same thing, i.e. container. It has different flags to get output as per our requirement. The ‘docker ps’ command is quicker and easier to type.
Syntax:
$docker container ls --help
$docker ps –help
How to List Containers in Docker?
- We have to use the Docker CLI tool to execute the command to list the containers.
- We have two Docker commands that list the containers.
- The first one is ‘docker container ls’ and the second one is ‘docker ps’.
- If we run any of this command we only get the running containers on that host, however, the command has different options that we can specify to get the list of containers that we need.
Examples of Docker List Containers
Given below are the examples mentioned:
Example #1
We have multiple containers in different states like some containers are running and some are in exited status or any other state other than running. We want to list all the containers whether it is running or not.
a. Let’s create a few alpine containers as below:
Code:
$docker run -d alpine sleep 3600
$ docker run -d alpine
$ docker run -d alpine
Output:
b. Now, we can run either ‘docker container ls’ or ‘docker ps’ command to get the list of the containers as below:
Code:
$docker container ls
$docker ps
Output:
Explanation
- We have created 3 containers but both commands show only 1 container as by default it shows only running containers.
c. We have to use the ‘–all’ or ‘-a’ flag to list all the containers as below:
Code:
$docker container ls --all
$ docker container ls –a
$docker ps --all
$docker ps -a
Output:
Example #2
Let’s assume we have hundreds of containers running on a host and we want to see specific containers, for example, all containers that are exited, have a specific name, id, label, volume, network, etc.
a. Create few containers with labels and names as below:
Code:
$ docker run -d ubuntu sleep 3600
$ docker run -d alpine
$ docker run --label type=db -d redis
$ docker run -d --name test-con1 alpine
Output:
b. Let’s start filtering these containers with id first as below:
Code:
$ docker container ls -a -f "id= ebe9557e"
$ docker container ls -a -f "id=eb"
Output:
c. We can also filter on the basis of the name of the container as below:
Code:
$docker container ls -a -f "name=test-con1"
Output:
d. We can filter the container based on its labels as below:
Code:
$ docker container ls -f "label=type=db"
$ docker container ls -f "label=type"
Output:
Explanation
- In the above snapshot, the first command shows the containers which have label ‘type=db” and the second command shows the containers which have label “type” as key regardless of the value.
e. We can filter the containers based on its image as below:
Code:
$ docker container ls -a -f ancestor=alpine
Output:
Code:
$ docker container ls -a -f ancestor=3fd9065eaf02
Output:
f. We want to filter containers running before and after a specific container, we can use before and since filter as below:
Code:
$ docker container ls -a -f before=laughing_carson
$ docker container ls -a -f since=laughing_carson
Output:
There are other filters available as well.
Example #3
Learn the use of the ‘–format’ option. If we want to display a specific field of the container or format the output as per our need. We can format based on container ID, image ID, command, ports, etc.
a. If we want to display only container’s ID, then use –format option as below:
Code:
$docker container ls -a --format "{{.ID}}"
Output:
b. If we want to output name with the id then we have to add that field as below:
Code:
$docker container ls -a --format "{{.ID}} {{.Names}}"
Output:
c. We can use the ‘\t’ to make more room between the fields as below:
Code:
$ docker container ls -a --format "{{.ID}} \t {{.Names}}"
Output:
d. We can use multiple fields at the same time and any symbol if you want as shown below:
Code:
$docker container ls -a --format "{{.ID}} -> \t {{.Names}} -> \t {{.Status}}"
Output:
e. We can check labels or any other hidden fields that is not shown by default when we list the containers.
Code:
$ docker container ls -a --format "{{.ID}} \t {{.Names}} \t {{.Labels}} \t {{.Size}}"
Output:
Explanation
- In the above snapshot, only one container has a label i.e. type=db.
f. We can see that the output does not have column headers like ID, Name, Label, etc. We have to use the ‘table’ directives as below:
Code:
$docker container ls -a --format "table {{.ID}} \t {{.Names}} \t {{.Labels}} \t {{.Size}}"
Output:
It gives us a cleaner output as well.
Example #4
We want to list last n created containers. We can do it by using the ‘–last’ or ‘-n’ option as shown below:
Code:
$ docker container ls -n 2
Output:
The above example shows the last 2 created containers and also includes all containers.
Example #5
We can use the ‘-l’ flag to list the latest created container. It shows all the containers regardless of their state without specifying the ‘-a’ flag.
Code:
$docker container ls -l
Output:
Example #6
Sometimes we have issues with the output due to screen size or more columns so by default Docker daemon truncated the output of some columns as below:
Code:
$docker container ls
Output:
Explanation
- In the above snapshot, we see that the ‘COMMAND’ column has been truncated.
We can use the ‘–no-trunc’ option to display full value of that column as below:
Code:
$docker container ls --no-trunc
Output:
We can now see the full container ID and command of the container. We can use this option with the other options as well like format:
Example #7
If we want to list container ID, we can only use the option ‘–quiet’ or ‘-q’. It is similar to using the ‘–format’ option with ID only. We can pipe it to the ‘wc’ command to count the containers as shown below:
Code:
$docker container ls --quiet
$ docker container ls -a –quiet
$ docker container ls -a –quiet | wc
Output:
Example #8
If we want to display the size of the containers, we have to use the ‘–size’ or ‘-s’ option as shown below. However, we can also output the same thing using the ‘format’ option, but this command is quicker.
Code:
$ docker container ls -a --size
Output:
Conclusion
We can specify multiple options to get the list of containers however most useful options are the ‘filter’ and the ‘format’ option. We often use the ‘docker ps’ command as we have to type less.
Recommended Articles
This is a guide to Docker List Containers. Here we have discussed how to list containers in docker with examples respectively. You may also have a look at the following articles to learn more –
4 Online Courses | 3 Hands-on Projects | 11+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses