Mastering Docker: How to Efficiently List All Containers with Command Line Precision
Forget the basics—discover how mastering the docker ps
command variations can transform your container oversight from chaotic to controlled, making downtime and mismanagement relics of the past.
If you work with Docker on any level—whether for local development, staging, or production—you already know how central container management is to your workflow. While starting and stopping containers is a fundamental skill, knowing what containers are running or have existed on your system is equally vital. This knowledge helps you debug issues, optimize system resources, and maintain a tidy environment.
In this practical guide, we’ll zero in on how to efficiently list all Docker containers from the command line with precision and clarity—including those currently running and those that have stopped.
Why Listing All Containers Matters
You might wonder: “Why not just look at running containers? Isn’t that enough?”
- Debugging stopped containers: Sometimes containers crash or stop unexpectedly; knowing their status and logs is key.
- Resource optimization: Stopped containers still consume disk space unless cleaned up.
- Audit & maintenance: To know what's been used or left orphaned after tests/deployments.
- Automation & scripting: Accurate container lists feed into scripts for backup, cleanups, or reporting.
Now let’s cut through the noise and learn exactly how to get that comprehensive container list at your fingertips.
The Basic Command to List Running Containers
The typical go-to command is:
docker ps
This shows all currently running containers in a concise table format.
Example output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ab123cd45ef6 nginx:latest "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:80->80/tcp confident_wilson
But this doesn't include any stopped or exited containers — only those actively running at the moment.
Listing All Containers with docker ps -a
To see everything—running and stopped containers—you need the -a
(or --all
) flag:
docker ps -a
Example output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ab123cd45ef6 nginx:latest "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:80->80/tcp confident_wilson
cd456ef789ab ubuntu "/bin/bash" 3 hours ago Exited (0) 30 minutes ago nostalgic_kowalevski
ef789ab123cd redis "docker-entrypoint.s…" 5 hours ago Exited (137) 2 hours ago vibrant_hodgkin
The key difference here is that you now see every container that has been run on your host—including those healthy and running as well as those exited or stopped.
Filtering Containers by Status
If you want to be more targeted—say only see exited containers—you can use the --filter
flag alongside docker ps
:
docker ps -a --filter "status=exited"
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cd456ef789ab ubuntu "/bin/bash" 3 hours ago Exited (0) 30 minutes ago nostalgic_kowalevski
ef789ab123cd redis "docker-entry…" 5 hours ago Exited (137) 2 hours ago vibrant_hodgkin
Statuses you can filter by include:
created
restarting
running
removing
paused
exited
dead
Using filters alongside listing allows you to quickly pinpoint exactly what state containers are in without sifting through everything manually.
Customizing Output Format with --format
For scripting or streamlined outputs, Docker offers Go templating for formatting outputs via the --format
option.
Example — list all container IDs and names only:
docker ps -a --format "{{.ID}}\t{{.Names}}"
Sample result:
ab123cd45ef6 confident_wilson
cd456ef789ab nostalgic_kowalevski
ef789ab123cd vibrant_hodgkin
This makes it effortless to consume container info programmatically—for example in shell loops or integrated scripts like cleanup routines.
Bonus Tips for Cleaning Up After Listing
After identifying all your exited/stopped containers, you may want to clean them up to free resources.
Delete all exited containers:
docker rm $(docker ps -a -q -f status=exited)
Explanation:
docker ps -a -q -f status=exited
: lists container IDs of exited containers quietly (-q
)- This list feeds into
docker rm
which removes each listed container
Be cautious — ensure you don’t remove anything important accidentally!
Summary Cheat Sheet
Command | Purpose |
---|---|
docker ps | List only running containers |
docker ps -a | List all containers including stopped ones |
docker ps -a --filter "status=exited" | List only exited/stopped containers |
docker ps -a --format "{{.ID}}\t{{.Names}}" | Custom formatted output for scripting |
Mastering listing Docker containers is more than a convenience — it’s a gateway to effective container lifecycle control. Use these command line gems not just as lookups but as tools for smarter debugging, resource cleanup, and automation that keep your environments lean and reliable.
Next time you suspect cluttered or invisible Docker remnants, reach for these commands first—and turn chaos into control!
Happy Dockering! 🚢
Need more Docker tips? Follow along for hands-on guides on optimizing container workflows.