- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to list containers in Docker?
Managing multiple Docker containers in a single host machine through a single command line can become tough. Hence, it’s better to know the Docker commands to manage containers the best possible way. Docker provides us with many command line tools and utilities to manage containers. In this article, we will discuss how to list Docker containers through multiple ways. We will also look at how to filter the list output to get the desired results. So without any further ado, let’s get started.
Listing Docker Containers
Predominantly, there are two major commands that you can use to display a list of all containers. These are -
- Docker container ls
- Docker ps
Both these commands can be used to achieve similar results and can be used interchangeably. Let’s check out the syntaxes of both these commands.
$ docker container ls [OPTIONS]
$ docker ps [OPTIONS]
You can use several options with the Docker container ls and Docker ps commands. Let’s discuss each one of them.
|--all||-a||You can use this option to display all containers. By default, the daemon only displays running containers.|
|--filter||-f||You can use this option to provide filters to the output depending on the conditions that you have provided.|
|--format|| ||You can pretty-print the list of containers by specifying a Go template.|
|--last||-n||If you want to display only the n last started containers, you can use this option.|
|--latest||-l||You can use this option to display a list of only the latest built containers.|
|--no-trunc|| ||To avoid truncating the output, you can use this option.|
|--quiet||-q||You can use the quiet option to only display the container IDs.|
|--size||-s||You can use this option to display the file sizes of containers.|
Example 1. Display Running Containers
If you want to display all the running containers, you can use the above-mentioned commands as it is.
$ docker container ls
$ docker ps
This will list only those containers that are actively running on your system. This will display parameters such as container ID, name of the container, associated image, date of creation, status of the container, ports that have been exposed, and the default command.
Example 2. List all Docker Containers
If you want to list all Docker containers (inactive or active), you can use the --all option along with the above-mentioned commands. This will list all the containers in all the states.
$ docker container ls -a
$ docker ps -a
Example 3. List Stopped Containers
Stopped containers are those containers that are in exited state. Containers are put into exited state by executing the Docker stop command in them. If you want to list only the stopped containers, you can use the --filter option with a parameter called status. Let’s see how to do so.
$ docker container ls --filter "status=exited"
Other status filters that you can provide along with the filter option is -
- Created - This means that the container was only created and not started.
- Restarting - This state means that the container is being restarted.
- Running - This means that the container is actively running.
- Paused - This means that all the processes inside the container have been paused.
- Exited - This means that the container has been stopped.
- Dead - This means that an attempt to stop the container failed.
In fact, you can also filter images on the basis of container ID, image, and name.
Example 4. Display Containers of an Image
If you want to display a list of containers associated with a particular image, you can use the filter option along with the ancestor parameter. Let’s see how to do so.
$ docker container ls -a --filter "ancestor=<name of image>"
Example 5. Print only Container ID
If you want to print only the ID of the containers, you can use the quiet option to do so. Here’s the command to print only the container IDs of all the containers.
$ docker container ls -a -q
If you want to display helpful information regarding both the commands, you can use the --help option as discussed below.
$ docker container ls --help
$ docker ps --help
Example 6. List Containers Created Before and After a Container
You can also use the filter option to list all the containers created before or after a particular container.
$ docker container ls -a -f before=container_name
$ docker container ls -a -f since=container_name
To sum up, in this article we discussed the two major commands that are used to display the complete list of containers in our host machine. Moreover, we discussed the various options that can be used with these commands to filter the result as per our requirements. We hope that you will now be able to work with the container list commands in a better and easier way.