docker ps Command
The docker ps
command is the primary tool for tracking container status in Docker. It provides real-time information about running and stopped containers.
## List running containers
docker ps
## List all containers (including stopped)
docker ps -a
## Filter containers by status
docker ps -f status=running
docker ps -f status=exited
Container Status Filtering Options
Filter Option |
Description |
status=running |
Show only running containers |
status=exited |
Show only stopped containers |
status=paused |
Show paused containers |
--format |
Custom output formatting |
Advanced Tracking with Docker Inspect
## Detailed container inspection
docker inspect [container_id]
## Extract specific container state information
docker inspect --format='{{.State.Status}}' [container_id]
Docker Events
## Monitor container lifecycle events
docker events
flowchart LR
A[Docker Events] --> B{Container Actions}
B --> |Create| C[Container Created]
B --> |Start| D[Container Started]
B --> |Stop| E[Container Stopped]
B --> |Die| F[Container Terminated]
Third-Party Monitoring Solutions
Docker Stats Command
## Real-time resource usage statistics
docker stats
## Limit to specific containers
docker stats container1 container2
Logging and Status Tracking
## View container logs
docker logs [container_id]
## Follow log output in real-time
docker logs -f [container_id]
Programmatic Status Tracking
Docker SDK for Python Example
import docker
client = docker.from_env()
for container in client.containers.list():
print(f"Container: {container.name}")
print(f"Status: {container.status}")
Best Practices for Status Tracking
- Use multiple tracking methods
- Implement automated monitoring
- Set up alerts for critical status changes
At LabEx, we emphasize the importance of comprehensive container status tracking for robust container management.