List all the docker containers
In the previous articles, we learned about how to build and run dockers with various options. (https://www.golinuxcloud.com/docker-run-i/ and https://www.golinuxcloud.com/docker-build-no-cache/ )
How do you know the status of your docker containers? Or how do you list all the docker containers?
Well, docker provides an option for you. That is to list the docker containers using docker CLI.
docker ps
is a command which lists the containers. In this article, let us understand more about this command. Let us get started.
Syntax
$ docker ps [OPTIONS]
To know more about the docker ps command, you can type the below command
$ docker ps --help
The docker ps command provides various flags which can serve different purposes and a few are mentioned below.
“ps” refers to the process state in Linux and in the same context, docker containers are also running as a process on a Linux server.
Options | Description |
--all , -a |
List all the docker containers. |
--quiet , -q |
List only the container IDs |
--filter , -f |
Filter the output based on the conditions |
--latest , -l |
Display the latest created containers |
More about docker ps -a
This command is used to show both the stopped and running docker containers.
By default, docker ps shows only the running containers. However, if you want to list all the available containers, use the -a
or --all
flag.
What happens when you run docker ps -a?
Whenever the user types the command, the docker CLI interacts with the docker daemon and makes an API call that internally processes the request and sends back the response to the standard output or the terminal.
Setup a Lab environment
In this section, let us learn about using the docker ps
command by running different docker containers.
Run the Alpine docker image
$ docker run -d alpine sleep 500
Run the Ubuntu docker image
$ docker run ubuntu
Run the Redis docker image
$ docker run -d redis
Now, let us see the status of the docker containers.
$ docker ps
When you enter the docker ps command, it lists the running docker containers. In the output, you can see that the Alpine container and Redis container has been up and running
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a380a419f9b9 alpine "sleep 500" 3 minutes ago Up 2 minutes sad_kalam
9b458ca21ed5 redis "docker-entrypoint.s…" 12 minutes ago Up 11 minutes 6379/tcp bold_chatterjee
50ca135be0c6 test-restart:1.0 "/bin/sh -c '/bin/ba…" 4 weeks ago Restarting (1) 2 seconds ago dazzling_roentgen
docker ps -a example
Up next, let us see the status of all the docker containers using docker ps with --all or -a flag.
$ docker ps -a
With this command, you will be able to see all the docker containers that are either in running, exited, created, or stopped state. Notice that the Alpine and Ubuntu containers have been exited and the Redis container is up and running.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a380a419f9b9 alpine "sleep 500" 8 minutes ago Exited (0) 22 seconds ago sad_kalam
9b458ca21ed5 redis "docker-entrypoint.s…" 17 minutes ago Up 17 minutes 6379/tcp bold_chatterjee
69d2661bcf4f alpine "sleep 500" 19 minutes ago Exited (0) 11 minutes ago friendly_mendeleev
e1a0aeb999d7 ubuntu "bash" 19 minutes ago Exited (0) 19 minutes ago funny_maxwell
Summary
Docker provides an easy way to know which containers you are running and the status of those. This command can be used by the developers to debug and know the status easily. With various combinations of the flags, it becomes easier to filter and format the output of docker commands as well.
There is an alternative to list the docker containers which is docker container ls. I hope this article helped you understand how to list all the available docker containers on the host. Please try it out and let us know in case you have any queries.
Happy learning!
References
https://docs.docker.com/engine/reference/commandline/ps/