Monitoring Docker container metrics using cAdvisor

cAdvisor (short for container Advisor) analyzes and exposes resource usage and performance data from running containers. cAdvisor exposes Prometheus metrics out of the box. In this guide, we will:

  • create a local multi-container Docker Compose installation that includes containers running Prometheus, cAdvisor, and a Redis server, respectively
  • examine some container metrics produced by the Redis container, collected by cAdvisor, and scraped by Prometheus

Prometheus configuration

First, you’ll need to configure Prometheus to scrape metrics from cAdvisor. Create a prometheus.yml file and populate it with this configuration:

  1. scrape_configs:
  2. - job_name: cadvisor
  3. scrape_interval: 5s
  4. static_configs:
  5. - targets:
  6. - cadvisor:8080

Docker Compose configuration

Now we’ll need to create a Docker Compose configuration that specifies which containers are part of our installation as well as which ports are exposed by each container, which volumes are used, and so on.

In the same folder where you created the prometheus.yml file, create a docker-compose.yml file and populate it with this Docker Compose configuration:

  1. version: '3.2'
  2. services:
  3. prometheus:
  4. image: prom/prometheus:latest
  5. container_name: prometheus
  6. ports:
  7. - 9090:9090
  8. command:
  9. - --config.file=/etc/prometheus/prometheus.yml
  10. volumes:
  11. - ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
  12. depends_on:
  13. - cadvisor
  14. cadvisor:
  15. image: gcr.io/cadvisor/cadvisor:latest
  16. container_name: cadvisor
  17. ports:
  18. - 8080:8080
  19. volumes:
  20. - /:/rootfs:ro
  21. - /var/run:/var/run:rw
  22. - /sys:/sys:ro
  23. - /var/lib/docker/:/var/lib/docker:ro
  24. depends_on:
  25. - redis
  26. redis:
  27. image: redis:latest
  28. container_name: redis
  29. ports:
  30. - 6379:6379

This configuration instructs Docker Compose to run three services, each of which corresponds to a Docker container:

  1. The prometheus service uses the local prometheus.yml configuration file (imported into the container by the volumes parameter).
  2. The cadvisor service exposes port 8080 (the default port for cAdvisor metrics) and relies on a variety of local volumes (/, /var/run, etc.).
  3. The redis service is a standard Redis server. cAdvisor will gather container metrics from this container automatically, i.e. without any further configuration.

To run the installation:

  1. docker-compose up

If Docker Compose successfully starts up all three containers, you should see output like this:

  1. prometheus | level=info ts=2018-07-12T22:02:40.5195272Z caller=main.go:500 msg="Server is ready to receive web requests."

You can verify that all three containers are running using the ps command:

  1. docker-compose ps

Your output will look something like this:

  1. Name Command State Ports
  2. ----------------------------------------------------------------------------
  3. cadvisor /usr/bin/cadvisor -logtostderr Up 8080/tcp
  4. prometheus /bin/prometheus --config.f ... Up 0.0.0.0:9090->9090/tcp
  5. redis docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp

Exploring the cAdvisor web UI

You can access the cAdvisor web UI at http://localhost:8080. You can explore stats and graphs for specific Docker containers in our installation at http://localhost:8080/docker/<container>. Metrics for the Redis container, for example, can be accessed at http://localhost:8080/docker/redis, Prometheus at http://localhost:8080/docker/prometheus, and so on.

Exploring metrics in the expression browser

cAdvisor’s web UI is a useful interface for exploring the kinds of things that cAdvisor monitors, but it doesn’t provide an interface for exploring container metrics. For that we’ll need the Prometheus expression browser, which is available at http://localhost:9090/graph. You can enter Prometheus expressions into the expression bar, which looks like this:

Prometheus expression bar

Let’s start by exploring the container_start_time_seconds metric, which records the start time of containers (in seconds). You can select for specific containers by name using the name="<container_name>" expression. The container name corresponds to the container_name parameter in the Docker Compose configuration. The container_start_time_seconds{name=”redis”} expression, for example, shows the start time for the redis container.

NOTE: A full listing of cAdvisor-gathered container metrics exposed to Prometheus can be found in the cAdvisor documentation.

Other expressions

The table below lists some other example expressions

ExpressionDescriptionFor
rate(container_cpu_usage_seconds_total{name=”redis”}[1m])The cgroup‘s CPU usage in the last minuteThe redis container
container_memory_usage_bytes{name=”redis”}The cgroup’s total memory usage (in bytes)The redis container
rate(container_network_transmit_bytes_total[1m])Bytes transmitted over the network by the container per second in the last minuteAll containers
rate(container_network_receive_bytes_total[1m])Bytes received over the network by the container per second in the last minuteAll containers

Summary

In this guide, we ran three separate containers in a single installation using Docker Compose: a Prometheus container scraped metrics from a cAdvisor container which, in turns, gathered metrics produced by a Redis container. We then explored a handful of cAdvisor container metrics using the Prometheus expression browser.

This documentation is open-source. Please help improve it by filing issues or pull requests.