How to List Containers in Docker
- List All Running Containers in Docker
- List Running and Exited Containers in Docker
- List a Specific Number of Containers in Docker
- List Only the Exited Containers in Docker
- List Recently Created Containers in Docker
- List Containers in Terms of ID in Docker
- List Containers Alongside their Size in Docker
This article will explore various commands for listing containers created in our system. This means that we should have made some containers beforehand for these commands to return values.
List All Running Containers in Docker
We will start by listing running containers. To do this, we can use the docker ps
command.
$ docker ps
This command lists all the containers currently running, which can be seen under the STATUS column in the sample output below. As shown here, we can also determine the approximate amount of time that the container(s) have been up and running.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c955bac1a84 ubuntu "bash" About a minute ago Up About a minute musing_morse
1296f9b9d330 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp distracted_napier
We can also use the docker container ls
command to return the same output.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c955bac1a84 ubuntu "bash" 14 minutes ago Up 14 minutes musing_morse
1296f9b9d330 nginx "/docker-entrypoint.…" 15 minutes ago Up 14 minutes 80/tcp distracted_napier
Besides the STATUS, the following details are also returned.
- The CONTAINER ID, which is a unique identifier for the container;
- The CONTAINER IMAGE, i.e., the image that we have used to build that container;
- The COMMAND responsible for running the container;
- The PORT mappings between the host machine and the container.
List Running and Exited Containers in Docker
Besides listing running containers, we can also list both running and exited containers. We need to append the -a
tag to the command we used earlier.
By default, these commands only show running containers; however, using this tag can get the commands also to list exited containers.
Command:
$ docker ps -a
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
38086474cb6c debian "bash" 4 minutes ago Exited (0) 2 minutes ago epic_jackson
1c955bac1a84 ubuntu "bash" 52 minutes ago Exited (0) About a minute ago musing_morse
1296f9b9d330 nginx "/docker-entrypoint.…" 53 minutes ago Up 58 seconds 80/tcp distracted_napier
In the output above under the Status column, you can note that the first two containers are not running while the last container is running.
List a Specific Number of Containers in Docker
We can also display a specific number of containers in our system. Suppose we want to show the first two containers only. We can do this by using the -n
tag alongside the commands that we used above.
This will bring up both running and exited but not more than the specified number.
Command:
$ docker container ls -n 2
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
38086474cb6c debian "bash" 31 minutes ago Exited (0) 29 minutes ago epic_jackson
1c955bac1a84 ubuntu "bash" About an hour ago Exited (0) 27 minutes ago musing_morse
Using the docker ps
command, in the same way, will also list the exact number of containers that we have specified below. Here is how we can implement that.
Command:
$ docker ps -a -n 2
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
38086474cb6c debian "bash" 33 minutes ago Exited (0) 31 minutes ago epic_jackson
1c955bac1a84 ubuntu "bash" About an hour ago Exited (0) 29 minutes ago musing_morse
List Only the Exited Containers in Docker
Using filters, we can also list only containers that are currently not running. Using the -f
tag to specify the condition that we want to be satisfied, in this case, we only wish to return containers with exited status.
We can also add multiple filters if we wish to return containers based on something else. Here is how we can list containers with exited status.
Command:
$ docker container ls --filter "status=exited"
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
38086474cb6c debian "bash" 51 minutes ago Exited (0) 49 minutes ago epic_jackson
1c955bac1a84 ubuntu "bash" 2 hours ago Exited (0) 48 minutes ago musing_morse
List Recently Created Containers in Docker
It is also possible to return the latest containers using the -latest
tag. This will return the current container(s) we have recently created.
Command:
$ docker container ls --latest
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
38086474cb6c debian "bash" 55 minutes ago Exited (0) 53 minutes ago epic_jackson
List Containers in Terms of ID in Docker
We can also list containers in terms of their IDs, and we can do so by using the -q
, also known as the quiet option, as shown below.
Command:
$ docker container ls -q
Output:
d780996c499a
f7509cd49142
72c8debe5efa
List Containers Alongside their Size in Docker
We can also list containers alongside their size. We can determine those taking up the largest memory size, among other details.
We can achieve that by using the -s
tag, also known as the size tag.
Command:
$ docker container ls -s
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
d780996c499a 4ac4842c584e "/opt/sonarqube/bin/…" 3 minutes ago Up 3 minutes 9000/tcp amazing_benz 73.2kB (virtual 520MB)
f7509cd49142 5285cb69ea55 "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 27017/tcp boring_wilson 0B (virtual 698MB)
72c8debe5efa rabbitmq "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 4369/tcp, 5671-5672/tcp, 15691-15692/tcp, 25672/tcp determined_curran 0B (virtual 221MB)
Isaac Tony is a professional software developer and technical writer fascinated by Tech and productivity. He helps large technical organizations communicate their message clearly through writing.
LinkedInRelated Article - Docker Container
- How to Check if the Docker Container Is Running or Not
- How to Restart a Docker Container
- The Difference Between Docker Container and Docker Image
- How to Delete Containers Permanently in Docker
- How to List Only the Stopped Containers in Docker
- How to Enter a Running Docker Container With a New Pseudo TTY