← All guides

Monitor Docker Container Resource Usage with docker stats

By Grzegorz Bukat, Founder of Zuzia · Updated:

Monitor Docker Container Resource Usage with docker stats

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 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'

Check Container Health and Restarts

docker inspect --format='{{.State.Health.Status}}' <container_name>   # only if the image defines a HEALTHCHECK
docker inspect --format='{{.RestartCount}}' <container_name>
docker ps -a --filter "status=restarting"

A container that keeps restarting often hits its memory limit, so compare the restart count with the memory limit above.

Get alerted when this changes

To stop checking by hand, save a command from this guide as a task in Zuzia, let it run on the server on a schedule and turn on "Monitor the result". The task alerts you when the command fails or its status line says WARNING or CRITICAL, by email, Slack, Discord, push or SMS, and the output of every run is kept. For that to work, the command has to fail when something is wrong, for example:

out=$(your_check_command 2>&1) || { echo "CRITICAL - $out"; exit 2; }
echo "OK - $out"

Exit 0 means OK, 1 a warning and 2 critical.

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:

  1. Adjust Resource Limits:

    # Update container with new limits
    docker update --memory="512m" --cpus="1.0" <container_name>
    
  2. Optimize Container Configuration:

    • Optimize container images
    • Reduce resource requirements
    • Implement resource quotas
  3. 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 runs the commands you give it on a schedule and alerts you when a command fails or its result changes.

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

We use cookies to ensure the proper functioning of our website.