Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
Listing Docker Containers
Predominantly, there are two major commands that you can use to display a list of all containers −
- docker container ls − Modern syntax for listing containers
- docker ps − Legacy command that works identically
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]
Available Options
You can use several options with the Docker container ls and Docker ps commands. The following table summarizes the key options −
| Name | Shorthand | Description |
|---|---|---|
| --all | -a | Display all containers (running and stopped). By default, only running containers are shown. |
| --filter | -f | Filter output based on provided conditions (status, name, image, etc.). |
| --format | Pretty-print containers using a Go template for custom output formatting. | |
| --last | -n | Show only the last n created containers (includes all states). |
| --latest | -l | Show only the latest created container (includes all states). |
| --no-trunc | Don't truncate output fields like container IDs and commands. | |
| --quiet | -q | Only display container IDs (useful for scripting). |
| --size | -s | Display total file sizes for each container. |
Examples
Example 1 − Display Running Containers
To display all currently running containers, use the basic command without any options −
$ docker container ls $ docker ps
This will display parameters such as container ID, name, associated image, creation time, status, exposed ports, and the default command.
Example 2 − List All Docker Containers
To list all Docker containers (both running and stopped), use the --all option −
$ docker container ls -a $ docker ps -a
Example 3 − List Containers by Status
To filter containers by their current state, use the --filter option with the status parameter −
$ docker container ls --filter "status=exited" $ docker container ls --filter "status=running"
Available status filters include −
- created − Container was created but not started
- restarting − Container is currently restarting
- running − Container is actively running
- paused − All processes in the container are paused
- exited − Container has stopped
- dead − Container daemon attempted to stop but failed
Example 4 − Display Containers by Image
To list containers created from a specific image, use the ancestor filter −
$ docker container ls -a --filter "ancestor=nginx" $ docker container ls -a --filter "ancestor=ubuntu:20.04"
Example 5 − Print Only Container IDs
To display only container IDs (useful for scripting), use the --quiet option −
$ docker container ls -a -q $ docker ps -aq
Example 6 − List Containers by Creation Time
You can filter containers created before or after a specific container using time-based filters −
$ docker container ls -a --filter "before=container_name" $ docker container ls -a --filter "since=container_name"
Advanced Filtering
Docker supports multiple filter types that can be combined for more precise results −
- name − Filter by container name
- id − Filter by container ID
- label − Filter by labels assigned to containers
- network − Filter by connected network
- volume − Filter by mounted volumes
$ docker container ls --filter "name=web*" $ docker container ls --filter "label=env=production"
Getting Help
For detailed information about available options, use the --help flag −
$ docker container ls --help $ docker ps --help
Conclusion
Docker provides flexible commands like docker container ls and docker ps to list and manage containers effectively. Using various options and filters allows you to quickly find specific containers based on status, image, name, or other criteria, making container management more efficient in complex Docker environments.
