How to Monitor Docker Container Resource Usage on Linux Server
Step-by-step guide to monitor Docker container resource usage. Track CPU, memory, disk, and network usage for containers to optimize performance and prevent resource exhaustion.
How to Monitor Docker Container Resource Usage on Linux Server
Monitor Docker container resource usage to track CPU, memory, disk, and network consumption, optimize container performance, and prevent resource exhaustion. This guide shows you how to monitor container resources and set up automated monitoring.
For comprehensive container monitoring, see Docker Container Monitoring. For troubleshooting container issues, see Docker Container Crash Restart Loop.
Why Monitoring Container Resources Matters
Container resource monitoring helps identify resource-intensive containers, optimize resource allocation, and prevent resource exhaustion that can cause container failures or poor performance.
Method 1: Monitor Container Resources with docker stats
Real-Time Container Resource Usage
# Monitor all containers in real-time
docker stats
# Monitor specific container
docker stats <container_name>
# Monitor containers without streaming
docker stats --no-stream
# Monitor with custom format
docker stats --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}"
Get Container Resource Summary
# Get resource usage for all containers
docker stats --no-stream --format "{{.Container}}: CPU {{.CPUPerc}}, Memory {{.MemUsage}}"
# Sort by CPU usage
docker stats --no-stream --sort cpu
# Sort by memory usage
docker stats --no-stream --sort mem
Method 2: Monitor Container Resources with docker inspect
Get Container Resource Limits
# Get container resource limits
docker inspect <container_name> | grep -A 10 "Resources"
# Get CPU limits
docker inspect <container_name> | grep -i cpu
# Get memory limits
docker inspect <container_name> | grep -i memory
Check Container Resource Usage Details
# Get container stats
docker stats <container_name> --no-stream
# Get container resource usage over time
watch -n 5 'docker stats <container_name> --no-stream'
Method 3: Automated Container Resource Monitoring with Zuzia.app
Set up automated monitoring to track container resources continuously and receive alerts when resource usage exceeds thresholds.
Step 1: Add Container Resource Monitoring Command
-
Log in to Zuzia.app Dashboard
- Access your Zuzia.app account
- Navigate to your server
- Click "Add Scheduled Task"
-
Configure Resource Monitoring Command
docker stats --no-stream --format "{{.Container}}: CPU {{.CPUPerc}}, Memory {{.MemUsage}}"- Set execution frequency (every 5-10 minutes)
- Configure alerts when resource usage exceeds thresholds
Step 2: Configure Alert Thresholds
- Warning: CPU usage > 80% or Memory usage > 80%
- Critical: CPU usage > 90% or Memory usage > 90%
- Emergency: CPU usage > 95% or Memory usage > 95%
Step 3: Monitor Specific Containers
Add commands to monitor critical containers:
# Monitor specific container
docker stats <container_name> --no-stream --format "{{.CPUPerc}} {{.MemUsage}}"
Best Practices for Container Resource Monitoring
1. Monitor All Containers Continuously
- Track resource usage for all containers
- Alert when resources exceed thresholds
- Monitor resource trends over time
- Optimize resource allocation based on data
2. Set Appropriate Resource Limits
- Configure CPU and memory limits for containers
- Monitor resource usage vs limits
- Adjust limits based on actual usage
- Prevent resource exhaustion
3. Monitor Resource Trends
- Review resource usage trends weekly
- Identify resource-intensive containers
- Optimize container configuration
- Plan capacity upgrades
4. Optimize Container Resources
- Adjust resource limits based on usage
- Optimize container images
- Implement resource quotas
- Scale containers when needed
Troubleshooting Container Resource Issues
Step 1: Identify Resource Problems
When container resources are exhausted:
# Check container resource usage
docker stats --no-stream
# Identify resource-intensive containers
docker stats --no-stream --sort cpu | head -10
docker stats --no-stream --sort mem | head -10
# Check container resource limits
docker inspect <container_name> | grep -A 10 "Resources"
Step 2: Resolve Resource Issues
Based on investigation:
-
Adjust Resource Limits:
# Update container with new limits docker update --memory="512m" --cpus="1.0" <container_name> -
Optimize Container Configuration:
- Optimize container images
- Reduce resource requirements
- Implement resource quotas
-
Scale Container Infrastructure:
- Add more containers if needed
- Distribute load across containers
- Optimize container orchestration
FAQ: Common Questions About Container Resource Monitoring
How often should I check container resources?
For production containers, continuous automated monitoring is essential. Zuzia.app can check container resources every few minutes, storing historical data and alerting you when resource usage exceeds thresholds.
What is considered high container resource usage?
High resource usage depends on your container limits and workload. Generally, CPU usage above 80% or memory usage above 80% of limits indicates potential issues and should be investigated.
How do I optimize container resource usage?
Optimize container resource usage by setting appropriate resource limits, optimizing container images, implementing resource quotas, monitoring resource trends, and scaling containers when needed.
Can container monitoring impact container performance?
Container monitoring commands have minimal impact on container performance when done correctly. Use appropriate monitoring frequency and avoid monitoring during peak usage periods.
Related guides, recipes, and problems
-
Related guides
-
Related recipes
-
Related problems