Monitoring the performance of your Docker containers is crucial for ensuring they run efficiently. The docker stats command provides real-time metrics of your running containers. In this guide, you'll learn how to use the docker stats command to monitor container performance with examples. Whether you're a beginner or an experienced user, this tutorial will enhance your Docker management skills.

Step 1: Understanding the docker stats Command

The docker stats command displays a live stream of resource usage statistics for your running containers. The basic syntax is:

docker stats [OPTIONS] [CONTAINER...]

If you don't specify any containers, docker stats will display metrics for all running containers.

Step 2: Viewing Metrics for All Containers

To see the performance metrics for all running containers, simply run:

docker stats

This command will display CPU usage, memory usage, network I/O, and block I/O for each container.

Step 3: Viewing Metrics for Specific Containers

To monitor specific containers, provide their names or IDs as arguments:

docker stats container_name_or_id

Example:

docker stats my_container

This command will display the performance metrics for the container named my_container.

Step 4: Using Options with docker stats

The docker stats command supports several options to customize the output. Some useful options include:

  • --all, -a: Show all containers (default shows just running).
  • --no-stream: Disable streaming stats and only pull the first result.

Example:

docker stats --no-stream my_container

This command will display a one-time snapshot of the metrics for my_container instead of a live stream.

Step 5: Interpreting the Metrics

  • CPU %: The percentage of the host’s CPU used by the container.
  • MEM USAGE / LIMIT: The amount of memory used by the container, and the limit of memory available.
  • MEM %: The percentage of the host’s memory used by the container.
  • NET I/O: The network input/output for the container.
  • BLOCK I/O: The block input/output for the container.
  • PIDS: The number of processes or threads created by the container.

Conclusion

The docker stats command is an invaluable tool for monitoring the performance of your Docker containers in real-time. By following this guide, you can efficiently track resource usage and ensure your containers are running optimally.

Additional Tips

  • Combine with Other Commands: Use docker stats in combination with other Docker commands to manage container performance effectively.
  • Automate Monitoring: Consider automating performance monitoring using scripts and integrating with monitoring tools.
  • Regular Checks: Regularly monitor your containers to prevent performance bottlenecks and resource issues.