List only stopped Docker containers
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In the realm of container orchestration and management, Docker stands out as one of the most widely used platforms. Managing Docker containers efficiently often involves listing, stopping, starting, and removing containers based on their states. This article delves into the technical intricacies of listing only those Docker containers that are stopped. By understanding these, you can maintain a cleaner and more efficient environment.
Understanding Docker Container States
Before diving into listing stopped containers, it is essential to understand the states a Docker container can be in:
- Created: The container has been created but not started.
- Running: The container is actively executing a command.
- Paused: The container is running but paused.
- Stopped (Exited): The container has been stopped and is not executing any command.
- Restarting: The container is in the process of restarting.
Listing Stopped Containers
Listing containers in a specific state, such as "stopped," allows administrators to manage container resources more effectively. Docker provides command-line utilities that enable users to filter containers by their state.
Using the docker ps Command
The docker ps command is primarily used to show a list of running containers. However, it can be extended with various options and filters to retrieve stopped containers:
-a: Shows all containers, not just running ones.--filter "status=exited": Filters the list to include only those containers that have stopped.
This command outputs container details, including CONTAINER ID, IMAGE, COMMAND, CREATED, STATUS, PORTS, and NAMES.
Example Output
Here's how the output might appear when listing stopped containers:
This output informs us about containers that have exited successfully or due to an error.
Reasons to List Stopped Containers
There are several reasons why you might want to list and manage stopped containers:
- Resource Management: Stopped containers still consume disk space and metadata resources. By identifying and removing them, you can free up valuable resources.
- Troubleshooting: Reviewing stopped containers can reveal errors or exit statuses that help diagnose issues with your containerized applications.
- Environment Cleaning: Regularly listing and removing stopped containers keeps your environment tidy and reduces clutter.
Additional Topics
Automating Cleanup with Docker Prune
For automated cleanup, use docker container prune to remove all stopped containers:
- This command will prompt for confirmation to delete all the stopped containers, effectively freeing up resources.
Risks of Unmonitored Containers
Ignoring stopped containers can lead to:
- Disk Space Bloat: Even stopped containers retain images and data that may consume disk space unnecessarily.
- Security Risks: Old containers may contain outdated or vulnerable software.
Key Commands Summary
Below is a tabular summary of key commands discussed regarding stopped Docker containers:
| Command | Description |
docker ps -a | List all containers, regardless of their state |
docker ps -a --filter "status=exited" | List only stopped (exited) containers |
docker container prune | Remove all stopped containers |
docker rm <container_id> | Remove a specific, stopped container |
Conclusion
Docker efficiently manages containerized environments, and part of that management involves effectively handling stopped containers. By utilizing the commands to list and potentially clean up these containers, administrators can ensure optimal use of system resources and maintain a streamlined environment. Regular maintenance, such as removing unused containers, not only conserves storage but also aligns with best security practices by eliminating potentially outdated components. Understanding and applying these techniques can enhance overall operational efficiency in Docker-managed infrastructures.

