In this quick guide, we will walk through the different ways to list containers in Docker, we will also see the options and filters that can help refine your search.
In order to list the Docker containers, we can use the “docker ps” or “docker container ls” command. This command provides a variety of ways to list and filter all containers on a particular Docker engine. Let's start by listing all the running containers.
Docker List Containers
Listing Running Containers
The primary command to list containers is:
docker ps
Or
docker container ls
The docker container ls command by default showcases only the containers that are running. This command provides a snapshot of details like the container ID, image name, creation time, status, ports, and the container's assigned name.
For example: If we use the “docker container ls” command with no options, it'll list all the running containers:
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1addfea727b3 mysql:5.6 "docker-en.." 2 seconds ago Up 1 second 0.0.0.0:32801->3306/tcp dazzling_hellman
09c4105cb356 nats:2.1.0-scratch "/nats-…" 17 minutes ago Up 17 minutes 4222/tcp, 6222/tcp, 8222/tcp nats-1
443fc0c41710 rabbitmq:3.7 "docker-…" 17 minutes ago Up 17 minutes 4369/tcp, 5671-5672/tcp, 25672/tcp rabbit-1
b06cfe3053e5 postgres:11 "docker-…" 29 minutes ago Up 29 minutes 0.0.0.0:32789->5432/tcp pg-2
4cf774b9e4a4 redis:5 "docker-…" 30 minutes ago Up 30 minutes 0.0.0.0:32787->6379/tcp redis-2
We have five running containers so far — Nats, RabbitMQ, PostgreSQL, MySQL, and Redis.
List All Containers
By default, the “docker container ls” command only shows the running containers. However, if we pass the -a or –all option, it'll list all (stopped and running) containers:
docker container ls -a
For example with output:
$ docker container ls -a
CONTAINER ID IMAGE STATUS
1addfea727b3 mysql:5.6 Up 4 hours
32928d81a65f mysql:5.6 Exited (1) 4 hours ago
09c4105cb356 nats:2.1.0-scratch Up 4 hours
443fc0c41710 rabbitmq:3.7 Up 4 hours
b06cfe3053e5 postgres:11 Up 4 hours
16d3c67ebd40 postgres:11 Exited (0) 4 hours ago
4cf774b9e4a4 redis:5 Up 4 hours
99c537a3dd86 redis:5 Exited (0) 4 hours ago
Latest Containers
To see the last n Docker containers (both running and stopped), we can use the -n <number> or –last <number> option:
$ docker container ls -n 2
CONTAINER ID IMAGE STATUS
1addfea727b3 mysql:5.6 Up 4 hours
32928d81a65f mysql:5.6 Exited (1) 4 hours ago
Filters for a Tailored View
Docker offers the feature of filtering the list of containers based on specific criteria. For instance, if you're only interested in containers spun from the 'nginx' image:
docker container ls -a --filter "ancestor=nginx"
Custom Output Formats
If the standard table format doesn't resonate with your needs, Docker lets you format the command's output. For example, if you're only after the container IDs:
docker container ls -aq
Or, if your focus is just on the names and IDs of the containers:
docker container ls --format "{{.ID}}: {{.Names}}"
Listing Containers by Their Status
You might find the need to view containers based on their statuses, like those that have exited or are currently paused: To see the containers that have exited:
$ docker container ls --filter "status=exited"
CONTAINER ID IMAGE STATUS
32928d81a65f mysql:5.6 Exited (1) 8 hours ago
16d3c67ebd40 postgres:11 Exited (0) 9 hours ago
99c537a3dd86 redis:5 Exited (0) 9 hours ago
For containers in a paused state:
$ docker container ls --filter "status=paused"
CONTAINER ID IMAGE STATUS
4cf774b9e4a4 redis:5 Up 45 minutes (Paused)
Conclusion
In this guide, we saw how to list and filter Docker containers using the “docker container ls” command and its useful options.
Related Container Management Guides
- Docker Create Container
- Docker Stop All Containers
- Docker Remove All Stopped Containers
- Docker Start Container
- Docker Restart All Containers
- Docker Go Inside Container - The docker exec Command
- Docker List Containers
- Docker Fetching Logs from Containers
- Docker Rename Container
- Docker Remove Unused Containers
Comments
Post a Comment
Leave Comment