Skip to content

Grafana Docker Compose stack with Prometheus

One compose file starts Grafana, Prometheus, Alertmanager, Node Exporter and cAdvisor. The data source, a Node Exporter dashboard and eight alert rules are already wired, so the first page you open has graphs on it.

git clone https://github.com/halitsever/grafana-docker-compose.gitcd grafana-docker-composecp .env.example .envdocker compose up -d

The dashboard you get

node-exporter.json

Panel titles, sizes and thresholds come from the provisioned JSON. The curves are a drawing, not live data.

After it starts

.env.example

Set GF_SECURITY_ADMIN_PASSWORD in .env before the first start, then open http://localhost:1296 and sign in as admin. Grafana is on host port 1296 here, not 3000.

The dashboard named Node Exporter is already in the list. Prometheus shows its three scrape jobs at http://localhost:9090/targets and the loaded rules at /alerts.

On Docker Swarm the same file works with docker stack deploy, with a few differences covered in the Swarm guide.

Services and host ports

docker-compose.yml
ServicePortImageJob in the stack
Grafana grafana1296grafana/grafana:13.3.0Dashboards and the web UI
Prometheus prometheus9090prom/prometheus:v3.16.0Scrapes, stores, evaluates rules
Alertmanager alertmanager9093prom/alertmanager:v0.35.0Groups and routes alerts
Node Exporter myserver_node_exporter9100prom/node-exporter:v1.12.1Host CPU, memory, disk, network
cAdvisor cadvisor8080gcr.io/cadvisor/cadvisor:v0.55.1Per-container metrics

Every service publishes its port on the host. Grafana maps 1296 to container port 3000. The rest use the same number on both sides.

How the five services connect

prometheus.yml
Docker host, all five ports publishedPromQLqueriesscrape every 5sjob nodescrape every 10sjob cadvisorfiring alertsprometheus/rules/node.yml8 rules, evaluated every 30sgrafanaUI on container port 3000data source and dashboard provisioned:1296prometheus3 scrape jobs, 30 day retentionscrapes itself every 15s:9090myserver_node_exporterreads the host / at /host, read-only:9100cadvisorprivileged, reads /sys and Docker data:8080alertmanagerreceivers: default, criticalemail and Slack blocks commented out:9093PromQL queriesscrape every 5s, job nodescrape every 10s, job cadvisorfiring alertsgrafanaUI on container port 3000data source and dashboard provisioned:1296prometheus3 scrape jobs, 30 day retentionscrapes itself every 15sevaluates 8 rules every 30s:9090myserver_node_exporterreads the host / at /host, read-only:9100cadvisorprivileged, reads /sys and Docker data:8080alertmanagerreceivers: default, criticalemail and Slack blocks commented out:9093
port published on the hostPrometheus scrapesalert path

Prometheus pulls metrics. It scrapes Node Exporter every 5 seconds, cAdvisor every 10 and itself every 15. Grafana never talks to the exporters. It queries Prometheus at http://prometheus:9090 through a provisioned data source.

Rules in prometheus/rules/node.yml are evaluated every 30 seconds. When one fires, Prometheus sends it to alertmanager:9093, which groups it and hands it to the default or critical receiver.

Alert rules

prometheus/rules/node.yml
  • criticalInstanceDownA scrape target stops answeringfor 1mup == 0
  • warningHighCpuUsageCPU above 90%for 5m100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 90
  • warningHighMemoryUsageMemory above 90%for 5m100 - ((node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100) > 90
  • criticalCriticalMemoryUsageMemory above 95%for 2m100 - ((node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100) > 95
  • warningDiskSpaceLowRoot filesystem above 85%for 5m100 - ((node_filesystem_avail_bytes{mountpoint="/",fstype!="tmpfs"} / node_filesystem_size_bytes{mountpoint="/",fstype!="tmpfs"}) * 100) > 85
  • criticalDiskSpaceCriticalRoot filesystem above 95%for 2m100 - ((node_filesystem_avail_bytes{mountpoint="/",fstype!="tmpfs"} / node_filesystem_size_bytes{mountpoint="/",fstype!="tmpfs"}) * 100) > 95
  • warningHighNetworkErrorsMore than 10 network errors per secondfor 5mirate(node_network_receive_errs_total[5m]) + irate(node_network_transmit_errs_total[5m]) > 10
  • warningSystemLoad15-minute load above 2x the CPU countfor 5mnode_load15 / count(node_cpu_seconds_total{mode="idle"}) without (cpu, mode) > 2

Alert routing

alertmanager.yml
group_by
alertname, instance
group_wait
30s
group_interval
5m
repeat_interval
12h
critical repeat_interval
1h
resolve_timeout
5m

Alerts labelled severity: critical go to the critical receiver and repeat every hour. Everything else lands on default and repeats every 12 hours.

Both receivers ship empty. The email and Slack webhook blocks are commented out, so no notification leaves the box until you add a receiver.

Guides

What is in the repository

The repository is configuration only. There is no application code and nothing to build. You get a compose file, a Prometheus config with three scrape jobs, one rule file, an Alertmanager config and Grafana provisioning for the data source and one dashboard.

Retention is 30 days by default and is set with PROMETHEUS_RETENTION. Prometheus runs with --web.enable-lifecycle, so a config change is applied with one HTTP call and no restart. To monitor another server, add its exporter as a second target under the node job.

CI checks every change with docker compose config, promtool check config and amtool check-config. The project is MIT licensed and is not affiliated with Grafana Labs, the Prometheus project or Docker.

MIT licensed. An independent project, not affiliated with Grafana Labs, the Prometheus project or Docker.