How to List Docker Containers?
Docker is a fundamental component in application development that simplifies building, deploying, and running applications using containers.
Learning how to list Docker containers is an important skill when using Docker. You can list all running and stopped containers with details about container ID, name, image, ports, and status using the docker ps -a
, docker container ls
, or docker container list
commands.
Comprehensive guide to list Docker containers
Working with Docker brings valuable advantages for developers; if you have not installed Docker on your server yet, by referring to our knowledge base, you can learn how to install Docker on a Linux server and configure Docker on a Windows server. Also, you need to buy VPS to optimize your Docker environments and usage.
While using Docker, listing Docker containers is a frequent task that you should accomplish in managing Docker containers to manage your workload optimally. Here are the most common methods to list Docker containers:
1. Listing only running containers:
To list containers that are currently running, use the following basic command:
docker ps
This command is the primary command to list all running docker containers, and it will display a table with detailed information about each active container, including container ID, image, command, creation time, status, ports, and name.
Example Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d9b100f2f636 nginx:latest "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 80/tcp brave_newton
a1b2c3d4e5f6 ubuntu:latest "/bin/bash" 10 minutes ago Exited (0) 8 minutes ago sleepy_kowalevski
Here are explanation of detailed information about each running container:
Container ID: The container’s unique ID is identity-mapped to each container, which is used to reference the container in other Docker commands.
Image: It is the name and tag of the Docker image used to create the container.
Command: It is a command run inside the container to start each container’s initialization and running process.
Created: It displays the time when the container was created.
Status: it displays the current status of the container. (“UP” indicates that the container is running, and “Exited (0)” indicates that the container is stopped.)
Ports: It shows details about port mappings between the host and the container.
Names: It shows a unique name assigned to each container.
2. Listing all containers (including stopped ones):
You can use docker ps
command with various options to optimize output and gain more details about Docker containers. The -a
flag with docker ps
command is used to show all containers regardless of their state (stopped, running, exited, paused, etc.):
docker ps -a
The output is similar to the docker ps command’s output, but it includes all containers.
From the status section in the output of this command, you can learn about the status of the containers. This command is helpful in identifying containers that have encountered issues and stopped.
3. Listing recently created container
If you need information about the containers you have recently created, use the -l
option with the docker ps
command. This option functions similarly to the -a
option but displays more details and lists recently created containers in addition to running or stopped containers:
docker ps -l
This command has not been supported in recent versions of Docker. Alternatively, you can use the docker ps -n N
command to list the N most recently created containers. For example, to list the last 4 created containers, use the following command:
docker ps -n 4
Identifying and checking the most recently created containers is effective for ensuring performance, optimizing resource management for the newest containers, and quick troubleshooting.
4. Listing only container IDs (Quiet Mode)
Sometimes you do not need detailed information about containers, fortunately, by using the -q (--quiet)
flag with the docker ps
command, you can list only container IDs:
docker ps -q
This command can be valuable for scripting purposes. Also, listing container IDs is helpful for orchestration tools to manage containers and create connections between containers.
5. Customizing output to display more details
Sometimes, information of the docker ps
command’s output is not adequate to use or difficult to understand due to truncation of the value of columns; therefore, for this purpose, you can disable truncation with the “--no-trunc
” option that allows you to get full container names, IDs, and other information:
docker ps --no-trunc
6. Showing container size
Checking the total file size of each container is good practice for optimal resource usage in scenarios where you have limited resources, and you must monitor the resource utilization by each container.
The -s (--size)
flag is another useful option to customize Docker container lists by adding an extra column that shows the total file size of each container:
docker ps -s
The output of this command gives you information about actual and virtual disk utilization by each container.
Actual size indicates disk utilization by the writable container layer, which includes the container’s filesystem and any customization.
The virtual size reports the combined size of all the container’s layers, including the writable layer, any shares, the image’s read-only layers, and additional layers.
7. Filtering Docker Containers
You can list specific containers based on various criteria such as name, label, ID, status, etc. All you need to do is use --filter
flags with key/value field pairs. ‘Key’ will be an attribute of the container, and ‘Value’ will be a condition you want to apply. Here are some common filter options:
Filter by container name, for example:
docker ps --filter "name=my-webserver"
Filter by container ID, for example:
docker ps --filter "id=container_id"
Filter by container state, for example:
docker ps --filter "status=running or created or exited"
Filter by image name, for example:
docker ps --filter "image=ubuntu:latest"
Filter by container label, for example:
docker ps --filter "label=com.example.version=1.0"
Filter by container network, for example:
docker ps --filter "network=bridge"
You can filter containers based on specific volumes, port numbers, and other criteria. Using the --filter
option requires having information about containers.
The --filter
option offers flexibility to applay advanced filters for listing containers. It allows you to combine some filter options; for example, to list currently stopped containers that publish port 80, run the following command:
docker ps --filter "status=exited" --filter "publish=80"
8. Formatting the Output
You can customize the output of docker ps
command format by adding --format
option.
The --format
option allows you to customize the output using Go templating language syntax to obtain more useful information.
By using the --format
option, you can list important features and desired information about the containers instead of the full output, enabling you to manage containers more effectively. Here is how it works:
docker ps --format "{{Field1}} {{Field2}}"
This command tailors the container list to the information you require. For example, if you want to get a list of containers with just the ID and status fields, run the following command:
docker ps --format "ID: {{.ID}}, Status: {{.Status}}"
Replace actual values in the command.
In addition, you can create your own table to effectively process container listings, using the following command:
docker ps --format "table {{. Field1 }}\t{{. Field2 }}\t{{. Field3 }}"
9. Listing containers using Docker compose
Docker Compose is the best platform to define multiple containers that work together in your application. So, if you benefit from Docker Compose, run the following command to list associated containers with your current Docker Compose project from a Compose project directory:
docker compose ps
This command, similar to the docker ps command, supports various options to expand its function.
10. Docker ps command alternatives
There are two alternatives for the docker ps command, which report similar output to the docker ps
command. Here are other methods to list Docker containers:
docker container list
docker container ls
These commands are useful for your scripts and documentation, but the docker ps command is the most commonly used method due to its short and memorable.
What’s the point of listing Docker containers?
- Checking container status is helpful to troubleshoot and resolve issues promptly by identifying stopped or failed containers.
- Ensuring all active containers and removing stopped containers is important for efficient resource management.
- By listing your containers, you can retrieve container details for other tasks.
- You can view ports that are currently in use by listing Docker containers.
- listing containers allows you to monitor container health check results to detect containers with issues.
- Listing containers, lets you write your own script.
How do I list all docker images?
To list all Docker images that are locally available on your system, run the following commands in your terminal:
docker images
Or
docker image ls
How to list all containers and delete docker?
If you want to delete all containers from your Docker, listing the Docker containers is beneficial. To remove all containers, including any currently running ones, first list all Docker containers:
docker ps -a
Therefore, delete all containers by running the following command:
docker rm $(docker ps -a -q)
Before running this command, ensure you do not need any containers because it will remove them.
How do I list unused docker containers?
To identify unused Docker containers (stopped containers) there are two methods:
- Listing all Docker containers including stopped ones:
docker ps -a
- Filtering Docker containers using
-f
flag and defining container condition:
docker ps -a -f "status=exited"
This command lists all containers with a status of “exited“.
How do I list all docker IPS?
The docker inspect command helps you to list the IP addresses of all Docker containers:
docker inspect -f '{{ .NetworkSettings.IPAddress }}' $(docker ps -q)
By using the docker ps -q
command, you can list all container IDs (quiet mode), and by running docker inspect -f '{{ .NetworkSettings.IPAddress }}'
command inspects each container ID, and extracts the IP address.
Conclusion
Docker, a popular containerization platform, enables developers to manage containers efficiently. Listing Docker containers is one of the essential tasks for developers working with Docker.
Being proficient in managing Docker containers offers advantages such as monitoring the status and debugging of what is running, optimal resource management, simplifying DevOps processes, container health monitoring, operational management, and automation and scripting.