Docker, a popular containerization platform, enables developers to build, ship, and run applications within isolated environments called containers. Managing these containers is a usuall task for Docker users. In this detailed blog, we’ll explore various commands to list Docker containers. We will be offering detailed explanations and sample outputs.
If you don’t want to get into the complexities of managing Docker, you can opt for our Docker consulting services. We designed them to make Docker implementation easy for you. Do check them out.
Table of Contents
Prerequisites
Before getting into the commands, ensure you have Docker installed on your system. If you haven’t already installed Docker, visit the official Docker website for installation instructions.
How to list docker containers?
Here are all the commands you will need to list docker containers
1. List Running Containers
To view the currently running containers on your system, use the following command:
The Output will look like this:
2. List All Containers (Including Stopped Ones)
To list all containers, including the ones that are not running, add the -a flag to the command:
The Output will look like this:
In this output, you can see both running and stopped containers. The ‘Exited’ status indicates that the container has stopped.
3. Display Container IDs Only
If you need just the IDs of the containers, you can use the –quiet or -q flag:
The Output will look like this:
This command provides a clean list of container IDs without any additional information.
Check Out: Our blog post on how to start and stop docker container
4. List Docker Container Names Only
To display only the container names, you can use the –format flag:
The Output will look like this:
This command exclusively lists the names of the containers running on your system.
5. List Docker Containers by Specific Hostname
If you want to filter containers based on a specific hostname, you can use the –filter flag:
The Output will look like this:
In this example, the command filters containers with the ancestor image “Supportfly” and displays relevant details.
6. List Latest Containers
To view the latest containers that were created, you can use the –latest or -n flag with the docker ps command. This shows the most recently created containers.
The Output will look like this:
In this output, you can see the most recently created container at the top.
Also Read: Our blog post on docker port mapping
7. Disable Truncation for Container IDs
By default, Docker truncates the container IDs in the output for readability. If you need the complete IDs, you can disable truncation using the –no-trunc flag.
The Output will look like this:
Disabling truncation provides the complete container IDs, ensuring you have accurate information for managing containers.
8. Quiet Mode – Display Container IDs Only
Quiet mode, enabled by the -q or –quiet flag, provides a minimalist output by displaying only the container IDs.
The Output will look like this:
Quiet mode is particularly useful when you need to pass container IDs as input to other commands or scripts, streamlining automation processes.
9. Combine Flags
You can combine these flags for more specific outputs. For example, to list the complete IDs of the latest containers quietly, you can use:
The Output will look like this:
By combining these flags, you can customize the output to suit your specific requirements, whether it’s for scripting, monitoring, or automation purposes.
Also Check: Our blog post on docker no space left on device
Advanced Techniques to list Docker containers
1. Display Container Sizes
To view the sizes of containers along with other details, you can use the –size or -s flag with the docker ps command.
The Output will look like this:
In this output, you can see the size of the container. The size is presented as the space occupied on the disk and the virtual size of the container.
2. Customized Output Format
.Docker allows you to customize the output format using the –format flag. You can specify a Go template to display specific information about containers.
The Output will look like this:
Customizing the output format enables you to focus on the essential information you need, making it easier to read and work with the data.
3. Advanced Filtering Options
Docker provides advanced filtering options to refine the list of containers based on specific criteria. Here are a few examples:
– Filter by Status (e.g., Running):
This command lists only the running containers on your system.
– Filter by Label:
This command filters containers by a specific label, allowing you to organize and manage your containers effectively.
– Filter by Network:
This command lists containers connected to the specified network, aiding in network-related troubleshooting and management tasks.
Conclusion
Effectively managing Docker containers is crucial for developers and system administrators. By learning these commands, you can efficiently list Docker containers. You will learn to filter them based on specific criteria and gain valuable insights into your Docker environment. Understanding these commands enables you to effectively manage your containerized applications. For more Docker-related tips and tricks, visit our blog section.