Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 37 ยท Concepts and Setup

Running Grafana with Docker Compose

Set up a local monitoring lab with Docker Compose: Grafana, Prometheus and node_exporter with volumes, a Prometheus scrape config and a provisioned data source.

Running containers one by one gets tedious. Docker Compose describes several services in one compose.yaml file and starts them together. This lesson builds a small monitoring lab: Grafana, Prometheus and a host exporter. You can reuse it for every later chapter.

These files run on your machine with Docker, not on this site. The YAML below is real and complete, but image tags change; pin the versions you tested.

The folder layout

TEXT
monitoring-lab/
  compose.yaml
  prometheus/
    prometheus.yml
  grafana/
    provisioning/
      datasources/
        prometheus.yml

compose.yaml

YAML
services:
  prometheus:
    image: prom/prometheus:latest
    volumes:
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - prometheus-data:/prometheus
    ports:
      - "9090:9090"

  node-exporter:
    image: prom/node-exporter:latest
    ports:
      - "9100:9100"

  grafana:
    image: grafana/grafana-oss:latest
    depends_on:
      - prometheus
    environment:
      GF_SECURITY_ADMIN_USER: admin
      GF_SECURITY_ADMIN_PASSWORD: change-me-please
      GF_USERS_DEFAULT_THEME: dark
    volumes:
      - grafana-data:/var/lib/grafana
      - ./grafana/provisioning:/etc/grafana/provisioning:ro
    ports:
      - "3000:3000"

volumes:
  prometheus-data:
  grafana-data:

Replace latest with real version tags in anything you keep. Inside the Compose network, services reach each other by service name: Prometheus is http://prometheus:9090 for Grafana, and node-exporter:9100 for Prometheus.

prometheus/prometheus.yml

YAML
global:
  scrape_interval: 15s

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ["prometheus:9090"]
  - job_name: node
    static_configs:
      - targets: ["node-exporter:9100"]

grafana/provisioning/datasources/prometheus.yml

YAML
apiVersion: 1
datasources:
  - name: Prometheus
    type: prometheus
    access: proxy
    url: http://prometheus:9090
    isDefault: true

Provisioning creates the data source automatically on startup (chapter 8 covers it in depth), so you do not need to click through the UI.

Start, stop, inspect

Terminal
docker compose up -d          # create and start everything
docker compose ps             # list services and ports
docker compose logs -f grafana
docker compose down           # stop and remove containers (volumes stay)
docker compose down -v        # also delete volumes (data is lost)

Then open http://localhost:3000, sign in with the admin credentials from the file, and check Connections > Data sources: Prometheus is already there. Open http://localhost:9090/targets to confirm both targets are UP.

On Docker Desktop for Windows or macOS, node-exporter reports the Linux VM's metrics, not your actual PC; that is fine for learning.

How the network fits together

flowchart LR
  B["Your browser"] -- ":3000" --> G["grafana"]
  G -- "prometheus:9090" --> P["prometheus"]
  P -- "node-exporter:9100" --> N["node-exporter"]

Grafana queries Prometheus; Prometheus scrapes the exporter. Only ports you publish are reachable from your machine.

Note the difference: with access: proxy, the Grafana server makes the request, so prometheus:9090 works. A URL typed for the browser, like localhost:9090, would point to Grafana's own container and fail.

Stand-in: validate a scrape config

Mistakes in scrape configs mean empty graphs. This stand-in checks a parsed config for common problems, the way you would review one by eye.

JS Stand-in: review scrape targets

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Stand-in: how often is a sample taken?

With a 15-second scrape interval, how many points exist in a range? This is why very short time ranges can look empty and why rate() windows must span several scrapes.

JS Stand-in: samples per range

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Common problems

  • Prometheus target is DOWN: wrong service name or port; check from inside the network.
  • Grafana cannot reach Prometheus: used localhost instead of the service name.
  • Data vanished: docker compose down -v removes volumes.
  • Port already used: change the left number of a mapping, e.g. "3001:3000".
  • Changes to provisioning are ignored: restart the Grafana container.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How does Grafana reach Prometheus inside a Compose network?

    Containers resolve each other by service name; localhost would mean the Grafana container itself.
  2. 2. What does docker compose down -v do beyond stopping containers?

    -v deletes volumes, so dashboards stored in grafana-data are lost.
  3. 3. Where do provisioning files for data sources go inside the Grafana container?

    Grafana reads /etc/grafana/provisioning/{datasources,dashboards,alerting,...} at startup.
  4. 4. What is a good minimum range for rate() with a 15s scrape interval?

    rate() needs at least two samples; four intervals tolerates a missed scrape.

Summary

  • Compose describes Grafana, Prometheus and an exporter in one file.
  • Services talk through service names; published ports are for your browser.
  • Provision the data source so a new lab is ready on first start.
  • Volumes keep data; down -v deletes it.
  • Check /targets in Prometheus before blaming Grafana.

Next: Time Series Data and Time Ranges