Collecting and scraping metrics
To collect and scrape metrics from containers running on a system with Docker Desktop installed, you can use Prometheus and Container Advisor (cAdvisor). Prometheus is a powerful open source monitoring and alerting toolkit, while cAdvisor provides container users with an understanding of the resource usage and performance characteristics of their running containers.
In this section, we’ll provide a step-by-step guide to setting up Prometheus and cAdvisor to collect and scrape metrics from containers running on Docker Desktop.
Step 1 – running cAdvisor in a Docker container
cAdvisor is a Google-developed tool that collects, processes, and exports container metrics. Let’s take a look:
- In the chapter folder,
ch12
, create a new subfolder calledmetrics
:mkdir metrics
- In this folder, create a file called
docker-compose.yml
and add the following snippet to it:version: '3.8' services: cadvisor: image: gcr.io/cadvisor/cadvisor:v0.45.0 container_name: cadvisor restart: always ports: - 8080:8080 volumes: - /:/rootfs:ro - /var/run:/var/run:rw - /sys:/sys:ro - /var/lib/docker/:/var/lib/docker:ro
- Run cAdvisor in a Docker container using the following command:
docker compose up cadvisor --detach
Replace
v0.45.0
with the latest cAdvisor version available on the cAdvisor repository.This command mounts the necessary directories from the host system and exposes cAdvisor’s web interface on port
8080
.
Attention
A version lower than the one shown here will not run, for example, on a Mac with an M1 or M2 processor.
- You can access the cAdvisor web interface by navigating to
http://localhost:8080
in your browser.
Step 2 – setting up and running Prometheus
Next, let’s set up Prometheus using the following step-by-step instructions:
- Create a subfolder called
prometheus
in themetrics
folder. - In this new folder, create a
prometheus.yml
configuration file with the following contents:global: scrape_interval: 15s scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'cadvisor' static_configs: - targets: ['host.docker.internal:8080']
This configuration specifies the global scrape interval and two scrape jobs: one for Prometheus itself and another for cAdvisor running on port
8080
. - Add the following snippet to the end of the
docker-compose.yml
file:prometheus: image: prom/prometheus:latest container_name: prometheus restart: always ports: - 9090:9090 volumes: - ./prometheus:/etc/prometheus - prometheus_data:/prometheus
This instruction mounts the
prometheus.yml
configuration file and exposes Prometheus on port9090
. - The preceding
prometheus
service uses a volume calledprometheus_data
. To define this, please add the following two lines to the end of thedocker-compose.yml
file:volumes: prometheus_data:
- You can access the Prometheus web interface by navigating to
http://localhost:9090
in your browser.
Once Prometheus is up and running, you can verify that it’s successfully scraping metrics from cAdvisor:
- Open the Prometheus web interface at
http://localhost:9090
. - Click on Status in the top navigation bar, then select Targets.
- Ensure that both the
prometheus
andcadvisor
targets are listed with State set toUP
.
Now, Prometheus can collect and store metrics from the containers running on your Docker Desktop system. You can use Prometheus’ built-in expression browser to query metrics or set up Grafana for advanced visualization and dashboarding:
- In the
query text
field, enter something likecontainer_start_time_seconds
to get the value for the startup time of all containers. - To refine the query and only get the value for the cAdvisor container, enter
container_start_time_seconds{job="cadvisor"}
.
Note that in the query text
field, you get IntelliSense, which is convenient when you do not remember all the details of a command and its parameters.
Before you continue, stop cAdvisor and Prometheus with the following command:
docker compose down -v
In the last section of this chapter, you will learn how to monitor a containerized application using a tool such as Grafana.