Here’s a complete tutorial on docker ps
, covering how it works, common options, examples, and a list of use cases.
What is docker ps
?
docker ps
is a Docker command used to list containers. By default, it shows only the running containers, but with additional options, you can view stopped, exited, or all containers.
Key Features:
- List all running containers.
- Filter by status, name, image, and ports.
- Helps in monitoring and managing containers.
- Use it to find container IDs for subsequent commands.
Basic Syntax
docker ps [OPTIONS]
Common Options:
-a
→ Show all containers (including stopped and exited ones).-q
→ Show only container IDs (useful for scripting).-l
→ Show the last created container (even if stopped).-n <number>
→ Show n most recent containers.--filter
→ Filter containers based on criteria (status, name, label, etc.).--format
→ Format the output using Go templates.--no-trunc
→ Don’t truncate the container’s output (show full IDs and names).
Examples of docker ps
1. List All Running Containers
docker ps
This shows only the containers that are currently running.
Example Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1b2c3d4e5f6 nginx "nginx -g…" 5 minutes ago Up 5 minutes 80/tcp my_nginx
2. List All Containers (Running, Stopped, and Exited)
docker ps -a
This shows all containers, including those that are stopped or exited.
3. Show Only Container IDs
docker ps -q
Example Output:
a1b2c3d4e5f6
b2c3d4e5f678
This is useful in scripts to perform operations on multiple containers.
4. Show the Last Created Container
docker ps -l
Displays the most recently created container, even if it has exited.
5. Show the 3 Most Recently Created Containers
docker ps -n 3
Lists the last three created containers.
6. Filter Containers by Status
docker ps --filter "status=exited"
This shows only containers that have exited.
Other possible status values:
running
paused
restarting
exited
7. Filter Containers by Name
docker ps --filter "name=my_nginx"
Lists containers with names that match my_nginx
.
8. Filter Containers by Image
docker ps --filter "ancestor=nginx"
Lists all containers created from the nginx
image.
9. Display Containers with Specific Ports
docker ps --filter "publish=8080"
Lists containers that expose port 8080
on the host.
10. Show Full Container IDs and Names
docker ps --no-trunc
Prevents truncation, showing full container IDs and names.
11. Format the Output (--format
)
docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"
This customizes the output to show only container ID, name, and status in a table format.
Example Output:
CONTAINER ID NAMES STATUS
a1b2c3d4e5f6 my_nginx Up 5 minutes
12. List Containers Created in the Last 24 Hours
docker ps --filter "since=24h"
13. Combine Filters
docker ps --filter "status=exited" --filter "ancestor=nginx"
This lists exited containers created from the nginx
image.
14. Use docker ps
in a Script to Stop All Running Containers
docker stop $(docker ps -q)
This stops all currently running containers.
15. Use docker ps
to Remove All Exited Containers
docker rm $(docker ps -aq --filter "status=exited")
This removes all containers that have exited.
Use Cases for docker ps
1. Monitoring Container Status
- Check which containers are running and their uptime.
- Ensure that critical services are active.
2. Managing Resources
- Find stopped containers and remove them to free up disk space.
- Identify paused or stuck containers for debugging.
3. Automation and Scripting
- Combine
docker ps
with other Docker commands (stop
,rm
,logs
, etc.) to automate maintenance tasks.
4. Debugging and Troubleshooting
- Filter by exited containers to find failed services.
- Use
--no-trunc
to get detailed information about containers for in-depth analysis.
5. Security and Auditing
- Audit all running containers to ensure they are up-to-date and secure.
- Monitor containers for unauthorized or unexpected behavior.
6. Multi-Service Applications
- Quickly check the status of multiple services running in containers.
- Use
docker ps
to verify that each service is running on its assigned port.
List of Common docker ps
Commands
Command | Description |
---|---|
docker ps | List running containers |
docker ps -a | List all containers |
docker ps -q | Show only container IDs |
docker ps -l | Show the last created container |
docker ps -n 5 | Show the last 5 created containers |
docker ps --filter "status=running" | Show only running containers |
docker ps --filter "ancestor=nginx" | List containers created from the nginx image |
docker ps --no-trunc | Show full container IDs and names |
docker ps --format "table {{.ID}}\t{{.Names}}" | Customize the output format |
Best Practices for Using docker ps
:
- Filter and format output for better readability and automation.
- Monitor container status regularly to ensure services are healthy.
- Use
docker ps -q
for scripting and batch operations. - Combine with
docker logs
anddocker inspect
for detailed troubleshooting. - Clean up unused containers (
docker ps -a
helps identify them).
Common Errors and Solutions
- “No such container”
→ The container may have stopped. Usedocker ps -a
to find it. - “Error response from daemon: Invalid filter”
→ Ensure the filter is valid. Check available filters withdocker ps --help
. - Command returns no output
→ There may be no running containers. Usedocker ps -a
to see stopped ones.
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND