Monitoring Docker Containers with cAdvisor

MetricFire
The MetricFire Blog
4 min readMar 30, 2020

--

Docker is one of the most popular tools for containerization, and several tools have been developed by the open-source community to monitor what happens inside of Docker containers. This guide focuses on one tool specifically: cAdvisor.

Why do we need to monitor Docker containers?

Monitoring allows us to gather vital information on the state of our software, enabling development teams to figure out ways in which to improve on their product. Because each container is a self-contained system, monitoring is even more imperative.

Monitoring for a container involves recording and reporting usage metrics from each container back to the monitoring system. This way, you ensure the detection of bugs and application refinement for better all-around performance and robustness.

How to use cAdvisor for container monitoring

cAdvisor (Container Advisor) is an open source container monitoring tool offered and managed by Google. It has native support for Docker containers and just about any other container. cAdvisor consists of a single container daemon that collects information about the containers that are running, processes that data, and then exports it. This information can go to its dedicated web interface, or to a third party app, such as Big Query, ElasticSearch, InfluxDB, Kafka, Prometheus, Redis, or StatsD. It can also be written to standard output by setting the -storage_driver flag to stdout.

Specifically, cAdvisor records historical resource usage, resource isolation parameters and network statistics for each container machine-wide.

Quick start: running cAdvisor

Setting up and running cAdvisor is a simple task because cAdvisor itself is a single Docker image. As such, it can be started and run using the docker run command. You can run cAdvisor to monitor containers on your local machine by running the following command in your terminal:

sudo docker run \— volume=/:/rootfs:ro \— volume=/var/run:/var/run:ro \— 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 \google/cadvisor:latest

It’s that easy! cAdvisor is now running in the background. It will connect itself to the Docker daemon running locally, gather the container usage metrics and display them on its web user interface accessible via http://localhost:8080/. On the user interface, you should see real time usage metrics from your containers including CPU and memory usage, a detailed overview of all your running processes, and so much more.

Notes:

  • If your docker daemon is running with user namespace enabled, you will need to add the — userns=host option to enable cAdvisor to monitor Docker containers. Otherwise, cAdvisor will be unable to connect to the Docker daemon.
  • If you’re running on either CentOS, Fedora or RHEL, you may need to run the container with — privileged=true and — volume=/cgroup:/cgroup:ro \ in order for cAdvisor to access and monitor Docker containers. This is because RHEL and CentOS are stricter when it comes to container access. With cAdvisor needing access to the Docker daemon through its socket, you will have to set — privileged=true. Also, in some versions of RHEL and CentOS the cgroup hierarchies are mounted in the /cgroup directory, so you will need the additional Docker option of — volume=/cgroup:/cgroup:ro \ to run cAdvisor.

Collecting Application Metrics with cAdvisor

In addition to container usage metrics, cAdvisor is also capable of collecting application metrics, including the number of active and reading connections, and whether or not the application has adequate CPU and memory allocation. (It is worth noting that, at the time of writing, application metrics support by cAdvisor is in the alpha stage of development, so keep that in mind.) A container can expose application metrics on a status page or on a separate API for fetching stats. cAdvisor provides a way to collect these metrics in a generic manner.

In order to enable cAdvisor to collect application metrics, there are two things you need to do:

  • Create a configuration
  • Pass that configuration to cAdvisor

Creating a configuration

An application metric configuration tells cAdvisor where to search for application metrics, and then specifies other parameters to export metrics from cAdvisor to user interfaces and backends. The metric configuration includes:

To finish reading this article, check out the full post on the MetricFire website.

--

--

MetricFire
The MetricFire Blog

Time series monitoring as a service using Graphite and visualized on Grafana.