Docker with Prometheus and Grafana: Monitoring made easy
Prometheus and Grafana are powerful tools for monitoring containerized applications and infrastructure. Docker simplifies the deployment and management of these tools, enabling instant monitoring, metric visualization, and alerting.
Overview
- Prometheus: An open source monitoring system that collects metrics from applications and infrastructure. It uses a pull-based mechanism and stores time series data.
- Grafana: A visualization tool for building interactive and customizable dashboards using data from Prometheus and other sources.
Why combine Prometheus and Grafana with Docker?
- Easy to set up: Docker provides pre-built images for Prometheus and Grafana, reducing setup complexity.
- portability: Execute Prometheus and Grafana in isolated containers to ensure consistency across environments.
- Scalability: Use orchestrators such as Docker Compose or Kubernetes to deploy containers on multiple hosts to easily expand monitoring settings.
- Integration: Monitor other Docker containers, services, and infrastructure elements.
Setting up Prometheus and Grafana using Docker
1. Prerequisites
- Install docker and Docker combination on your system.
- Basic understanding of Docker and container networking.
2. Define Docker Compose file
Create a docker-compose.yml
files to configure Prometheus and Grafana.
version: '3.8'
services:
prometheus:
image: prom/prometheus:latest
container_name: prometheus
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- "9090:9090"
grafana:
image: grafana/grafana:latest
container_name: grafana
ports:
- "3000:3000"
environment:
- GF_SECURITY_ADMIN_USER=admin
- GF_SECURITY_ADMIN_PASSWORD=admin
3. Configure Prometheus
Create a prometheus.yml
File to define Prometheus configuration.
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'docker'
static_configs:
- targets: ['host.docker.internal:9323']
In this example:
- Prometheus uses the following instructions to grab metrics from the Docker host Metrics endpoint for Docker Daemon (via port
9323
).
4. Run the stack
Use Docker Compose to run the Prometheus and Grafana stack:
docker-compose up -d
This will start:
- Prometheus Part 1
http://localhost:9090
- Grafana
http://localhost:3000
5. Add data sources to Grafana
- Visit Grafana:
http://localhost:3000
(Default username/password:admin/admin
). - Navigate to Configuration > Data Sources.
- Added Prometheus as a data source:
- URL:
http://host.docker.internal:9090
- Store and test.
- URL:
6. Import the dashboard
- Go to Build > Import In Grafana.
- Use pre-built dashboards (for example, Docker Monitoring Dashboard ID:
12210
). - Select Prometheus as the source and import.
Monitor Docker containers
1. Expose Docker metrics
Enable Docker metrics on the host by modifying the Docker Daemon configuration:
- Add the following to
/etc/docker/daemon.json
:
{
"metrics-addr": "0.0.0.0:9323",
"experimental": true
}
- Restart the Docker service:
sudo systemctl restart docker
2. Visualize container metrics
- Prometheus will scrape metrics from the Docker daemon.
- Use Grafana dashboards to visualize metrics such as:
- Container CPU and memory usage
- network traffic
- Disk input/output
Advanced configuration
Alerts in Prometheus
- Add alert rule
prometheus.yml
:
rule_files:
- "alert.rules"
alerting:
alertmanagers:
- static_configs:
- targets: ['host.docker.internal:9093']
- example
alert.rules
:
groups:
- name: example
rules:
- alert: HighCPUUsage
expr: container_cpu_usage_seconds_total > 80
for: 5m
labels:
severity: critical
annotations:
summary: "High CPU usage detected"
description: "Container {{ $labels.container }} is using high CPU."
Custom dashboards in Grafana
- use query builder or Prom QL Build custom panels and dashboards that fit your monitoring needs.
Scale with Docker Swarm or Kubernetes
Deploy Prometheus and Grafana on the orchestrator for better scalability and fault tolerance:
- Using Kubernetes helm chart to simplify setup.
- Integrate with monitoring tools such as Kubernetes Metrics Server.
best practices
-
Optimize scraping interval:
Adjust the crawl interval in Prometheus for high-traffic environments to avoid overloading the system. -
Use persistent storage:
Mount the volume to save data:
volumes:
- prometheus_data:/prometheus
- grafana_data:/var/lib/grafana
-
secure access:
- Grafana and Prometheus use HTTPS.
- Set up strong Grafana administrator credentials.
-
Automated deployment:
Automate deployment of monitoring stacks using CI/CD pipelines.
in conclusion
Deploying Prometheus and Grafana using Docker provides a powerful, flexible, and scalable monitoring solution. With the simplicity and containerization of Docker, it’s easy to set up real-time monitoring to ensure your infrastructure and applications are running at their best.