Listing All Running Containers
To list all running containers in Docker, you can use the docker ps
command. This command will display information about the currently running containers, including their container ID, image, command, creation time, status, and ports.
Here's the basic syntax for the docker ps
command:
docker ps [options]
The most common options used with docker ps
are:
-a
: Show all containers (running and stopped)-q
: Only display the numeric IDs of the containers-f
: Filter the output based on certain conditions
Here's an example of how to use the docker ps
command to list all running containers:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1b2c3d4e5f6 nginx:latest "/docker-entrypoint.…" 10 minutes ago Up 10 minutes 0.0.0.0:80->80/tcp web-server
g7h8i9j0k1l2 redis:latest "docker-entrypoint.s…" 15 minutes ago Up 15 minutes 0.0.0.0:6379->6379/tcp cache-server
This output shows that there are two running containers: a web server running the nginx:latest
image and a cache server running the redis:latest
image.
If you want to see all containers, including those that are stopped, you can use the -a
option:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1b2c3d4e5f6 nginx:latest "/docker-entrypoint.…" 10 minutes ago Up 10 minutes 0.0.0.0:80->80/tcp web-server
g7h8i9j0k1l2 redis:latest "docker-entrypoint.s…" 15 minutes ago Up 15 minutes 0.0.0.0:6379->6379/tcp cache-server
m3n4o5p6q7r8 ubuntu:latest "/bin/bash" 30 minutes ago Exited (0) 25 minutes ago test-container
This output shows the same two running containers, plus an additional container that has been stopped.
You can also use the docker ps
command to filter the output based on certain conditions. For example, to list only the container IDs of running containers, you can use the -q
option:
$ docker ps -q
a1b2c3d4e5f6
g7h8i9j0k1l2
Or, to list all containers with a specific image name, you can use the -f
option:
$ docker ps -a -f "image=nginx:latest"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1b2c3d4e5f6 nginx:latest "/docker-entrypoint.…" 10 minutes ago Up 10 minutes 0.0.0.0:80->80/tcp web-server
Here's a Mermaid diagram that illustrates the key concepts related to listing running Docker containers:
In summary, the docker ps
command is a powerful tool for listing and managing running Docker containers. By using the various options available, you can customize the output to suit your specific needs, whether that's seeing all containers, filtering by image name, or just getting the container IDs.