Docker Container Monitoring with cAdvisor, Prometheus, and Grafana using Docker Compose
In this blog post, we will explore Docker container monitoring and learn how to set up a complete Docker container monitoring solution with cAdvisor, Prometheus, and Grafana using Docker Compose.
Docker
Docker is an open-source platform that enables you to automate application deployment, scaling, and management with containerization. It provides a way to package an application with all of its dependencies into a standardized unit called a container.
A container is a lightweight, isolated environment that contains everything needed to run an application, including the code, runtime, system tools, and libraries.
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.
Install the Compose plugin: https://docs.docker.com/compose/install/linux/
Container monitoring typically involves collecting and analyzing metrics such as CPU usage, memory consumption, disk I/O, network traffic, container uptime, and application-specific metrics.
In this article, we will cover docker container monitoring by using cAdvisor, Prometheus, and Grafana.
cAdvisor
cAdvisor, short for Container Advisor, is an open-source monitoring and performance analysis tool specifically designed for Docker containers and other containerization platforms. It is a running daemon that collects, aggregates, processes, and exports information about running containers.
Prometheus
Prometheus is a free and open-source tool that helps keep an eye on your software systems, ensuring they are healthy and performing well. It collects important data about your applications and services, like how much they’re being used or if they’re running smoothly. If there’s a problem, Prometheus can raise alerts to let you know, so you can fix things before they become serious issues.
Grafana
Grafana is a user-friendly and open-source data visualization tool that helps you to turn complex data into easy-to-understand charts and graphs. It connects to various data sources, like databases or monitoring systems, and allows you to create interactive dashboards. With Grafana, you can visualize trends, track performance, and monitor the health of your applications or systems, making it easier to make informed decisions and spot potential problems quickly.
Here is a look at the file structure and docker-compose with all the configuration files.
File Structure
dir/
|- prometheus.yml
|- datasources.yml
|- docker-compose.yml
|- dashboard.json
|- default.yaml
The configuration files are added so a user does not have to do any configuration to set up Prometheus scrap configuration, Grafana datasources and dashboards.
Configuration Files
prometheus.yml datasources.yml dashboard.json default.yaml
docker-compose.yml
version: "3"
services:
cadvisor:
container_name: cadvisor
image: gcr.io/cadvisor/cadvisor:latest
network_mode: "host"
ports:
- "8080:8080"
volumes:
- "/:/rootfs"
- "/var/run:/var/run"
- "/sys:/sys"
- "/var/lib/docker/:/var/lib/docker"
- "/dev/disk/:/dev/disk"
privileged: true
devices:
- "/dev/kmsg"
prometheus:
container_name: prometheus
image: prom/prometheus:latest
network_mode: "host"
ports:
- "9090:9090"
volumes:
- "./prometheus.yml:/etc/prometheus/prometheus.yml"
privileged: true
depends_on:
- cadvisor
grafana:
container_name: grafana
image: grafana/grafana:latest
network_mode: "host"
ports:
- "3000:3000"
environment:
- GF_PATHS_PROVISIONING=/etc/grafana/provisioning
- DS_PROMETHEUS=prometheus
volumes:
- "grafana-data:/var/lib/grafana"
- "./datasources.yml:/etc/grafana/provisioning/datasources/datasources.yml"
- "./dashboard.json:/var/lib/grafana/dashboards/dashboard.json"
- "./default.yaml:/etc/grafana/provisioning/dashboards/default.yaml"
privileged: true
depends_on:
- prometheus
volumes:
grafana-data:
Command to run docker-compose.yml
docker compose up
Now you can access cAdvisor on http://localhost:8080, Prometheus on http://localhost:9090, and Grafana on http://localhost:3000.
I hope this article helps you to monitor your docker container.