Monitoring Docker Containers: Best Practices
Track container health, resource usage, and logs. Best practices for Docker monitoring in production environments.
> For people who run servers
Why Docker Monitoring is Different
Docker containers are ephemeral — they start, stop, and get replaced frequently. Traditional server monitoring tools don't always work well with containers. You need monitoring that understands the container lifecycle.
Key Metrics for Containers
Resource Usage per Container
# Built-in Docker stats
docker stats --no-stream
# Formatted output
docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}"Container Health
- Status: Running, Stopped, Restarting, Dead
- Restart count: High restart counts indicate instability
- Uptime: How long since last restart
- Health checks: Custom health check results
Image & Configuration
- Image version: Track which image version is deployed
- Port mappings: Verify correct port configuration
- Volume mounts: Ensure data persistence
- Network: Container network connections
Setting Up Docker Monitoring with ServerGuard
ServerGuard's agent automatically detects running Docker containers and monitors them:
# Install the agent - Docker monitoring included
curl -sSL https://safeavio.com/install.sh | sudo bash
# Verify Docker access
sudo docker psWhat Gets Monitored
- Container count (running, stopped, total)
- Per-container CPU and memory usage
- Network I/O per container
- Container logs (aggregated and searchable)
- Image pull status and updates
- Health check results
Best Practices
1. Set Resource Limits
Never run containers without resource limits in production:
# docker-compose.yml
services:
web:
image: nginx
deploy:
resources:
limits:
cpus: '1.0'
memory: 512M
reservations:
cpus: '0.25'
memory: 128M2. Use Health Checks
HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
CMD curl -f http://localhost/health || exit 13. Centralize Logs
# Docker daemon logging config
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}4. Monitor the Docker Daemon
The host running Docker needs monitoring too:
- Docker daemon CPU/memory usage
- Disk space (images can consume lots of space)
- Docker socket availability
- Container networking health
5. Clean Up Unused Resources
# Remove unused containers, images, networks
docker system prune -a --volumes
# Check disk usage
docker system dfAlerting Strategy
| Alert | Threshold | Priority |
|---|---|---|
| Container stopped | Any critical container | High |
| Restart loop | >3 restarts in 5min | High |
| Memory >90% of limit | Per container | Medium |
| CPU >80% sustained | Per container | Medium |
| Disk space >85% | Docker host | High |
| Health check failing | 3 consecutive | High |
Conclusion
Docker monitoring requires understanding both the container layer and the host layer. Use resource limits, health checks, and centralized monitoring to keep your containerized applications running smoothly. ServerGuard provides out-of-the-box Docker monitoring so you can focus on building, not babysitting containers.