Monitor Docker Container Resources with `docker stats` and cAdvisor: Identify CPU, RAM Hogs

Docker tutorial - IT technology blog
Docker tutorial - IT technology blog

Introduction to the Problem

Are you working with Docker and suddenly notice your application running unusually slow? Or perhaps the entire server is sluggish for no apparent reason? In an environment with many Docker containers running concurrently, identifying the ‘culprit’ consuming system resources (CPU, RAM) is extremely crucial. Without monitoring, optimizing or troubleshooting issues becomes very difficult.

Fortunately, Docker provides a powerful built-in tool, docker stats. The community has also developed cAdvisor, a more professional solution. This article will provide detailed instructions on how to use both. You will easily monitor, detect, and resolve container performance issues.

Core Concepts

What is docker stats?

docker stats is a built-in command in the Docker CLI. It allows you to view real-time resource usage information for running containers. This tool provides a quick overview of each container’s CPU, RAM, network I/O, and disk I/O. It’s an ideal choice for a quick check when you suspect performance issues.

What is cAdvisor?

cAdvisor (Container Advisor) is an open-source tool from Google. It’s designed to collect, aggregate, process, and export performance and resource information from running containers. Unlike docker stats, which only displays real-time data, cAdvisor offers superior capabilities. It provides intuitive charts and detailed historical data. This helps you analyze trends and detect long-term or recurring resource issues.

Why Monitor CPU and RAM?

CPU and RAM are two core resources that determine application performance. This holds true even when applications run within containers. If a container consumes too much CPU, it can slow down other processes on the same Docker host. Similarly, when RAM is excessively ‘gobbled up’, the system might be forced to use swap (exchanging data from RAM to disk). The consequence is a severe decrease in performance, potentially leading to ‘Out Of Memory’ (OOM) situations and causing the application to crash completely.

Detailed Practice

1. Quick Monitoring with docker stats

docker stats is the fastest tool to get a general overview of container resource status. Let’s open a terminal and practice the following commands!

Basic Command: View all containers

Just type docker stats without any additional arguments, and you will see a list of all running containers, along with detailed resource information for each.


docker stats

The returned result will look like this:


CONTAINER ID   NAME         CPU %     MEM USAGE / LIMIT   MEM %     NET I/O      BLOCK I/O      PIDS
8e1d5a7d0c3b   my-nginx     0.00%     2.207MiB / 7.769GiB   0.03%     1.57kB / 0B    0B / 0B        2
4b2e1f9c8d7a   my-app       0.23%     125MiB / 7.769GiB   1.61%     6.74MB / 1.2MB   1.5MB / 564kB   15
  • CONTAINER ID / NAME: The ID and name of the container.
  • CPU %: The percentage of CPU the container is using. If this value is consistently high (e.g., above 80%), it’s a sign that the container is overloaded or experiencing issues.
  • MEM USAGE / LIMIT: The amount of RAM the container is using compared to its allocated limit (if any).
  • MEM %: The percentage of RAM being used. Similar to CPU, a high number (e.g., above 70-80%) indicates a risk of insufficient RAM.
  • NET I/O: The amount of network data the container has sent and received.
  • BLOCK I/O: The amount of data read/written to disk.
  • PIDS: The total number of processes the container is currently running.

Monitor Specific Containers

To view only one or a few specific containers, simply add their IDs or names to the command:


docker stats my-nginx my-app

Get Non-Streaming Data (Once)

Sometimes, you only need a ‘snapshot’ of resource status, not a continuous data stream. In such cases, use the --no-stream flag:


docker stats --no-stream my-nginx

Practical Example with Nginx

Let’s try running a simple Nginx container and check its resources:


docker run -d --name my-nginx nginx

Then, run the command docker stats my-nginx. You’ll notice that Nginx’s CPU and RAM usage are typically very low when there’s no traffic. If you try to access Nginx via a browser, you might see a slight increase in CPU %.

2. In-depth Monitoring with cAdvisor

For deeper insights, historical tracking, and data visualization, cAdvisor is the perfect choice. Conveniently, cAdvisor also runs as a Docker container.

Install cAdvisor (running in a Docker container)

Run the following command to start cAdvisor. This command will download the cAdvisor image from Google Container Registry. It will then run and grant access to the necessary Docker system resources.


docker run \
  --volume=/:/rootfs:ro \
  --volume=/var/run:/var/run:rw \
  --volume=/sys:/sys:ro \
  --volume=/var/lib/docker/:/var/lib/docker:ro \
  --volume=/dev/disk/:/dev/disk:ro \
  --publish=8080:8080 \
  --detach=true \
  --name=cadvisor \
  gcr.io/cadvisor/cadvisor:latest

Explanation of arguments:

  • --volume=...:ro: Mount system directories in read-only mode. This allows cAdvisor to collect information about the system and other containers.
  • --volume=/var/run:/var/run:rw: Grant write access to the /var/run directory so cAdvisor can interact with the Docker daemon.
  • --publish=8080:8080: Open port 8080 on the host. This allows you to access cAdvisor’s web interface.
  • --detach=true: Run the container in the background.
  • --name=cadvisor: Name the container cadvisor for easier management.

Once the command runs successfully, cAdvisor will immediately start collecting data. You can access its web interface via your browser at http://localhost:8080. Or use http://<IP_DOCKER_SERVER>:8080 if you are running on a remote server.

Explore the cAdvisor Interface

When you access http://localhost:8080, the cAdvisor interface will appear. The Overview page displays general information about the CPU and Memory of the entire system. To view details for individual containers, simply navigate to the “Containers” tab on the left sidebar.

In the “Containers” tab, you will see a list of active containers. Click on any container (e.g., my-nginx or cadvisor) to view its detailed resource charts:

  • CPU Usage: The chart shows CPU utilization over time. It helps you identify peak or unusual periods.
  • Memory Usage: The RAM chart indicates whether the container is exceeding its threshold or showing signs of memory leaks.
  • Network Traffic: The network traffic chart. This is useful information when debugging applications that heavily rely on network I/O.

These charts are cAdvisor’s strong suit compared to docker stats. You can easily see trends and resource peaks, enabling you to make accurate optimization decisions based on historical data.

Detecting Resource-Hogging Containers

With cAdvisor, detecting resource-hogging containers becomes much more intuitive and easier. If a container’s CPU chart consistently shows high usage (e.g., above 80-90%) for an extended period, that’s a red flag. Similarly, if the Memory chart always approaches its limit (if any), it’s also a clear warning. At this point, you can:

  • Check that container’s logs. See if any errors are running in an infinite loop.
  • Consider increasing the resource limits (CPU/RAM) for the container if it genuinely needs more to operate stably.
  • Optimize the code or application configuration inside the container. The goal is to efficiently reduce resource load.

When debugging performance issues on Docker, especially when analyzing JSON responses from APIs or container configurations, I find reading raw JSON very annoying. I frequently use toolcraft.app/en/tools/developer/json-formatter to reformat it. This tool helps me understand API response data from cAdvisor or other APIs much faster. Moreover, it doesn’t require installing additional extensions, saving significant time.

Conclusion

Monitoring Docker container resources is an essential skill for anyone working with Docker. With docker stats, you can quickly check the current status of your containers. When you need in-depth analysis, trend tracking, or to uncover potential issues, cAdvisor is a powerful tool that provides intuitive and detailed insights.

Integrate this monitoring into your daily workflow. Early detection and resolution of resource-hogging containers not only lead to more stable applications but also significantly optimize server costs. Start implementing it now to master your containers and ensure your system always runs smoothly!

Share: