composebuilder

Docker Compose: Prometheus + Grafana + Node Exporter

Monitor a Linux host with Prometheus, Node Exporter and Grafana in Docker Compose. Pinned images, .env secrets, retention settings and localhost-only ports.

When to use this stack

Prometheus collects numeric metrics on a schedule, stores them as time series and answers queries written in PromQL. Node Exporter is the agent that exposes Linux host metrics in the format Prometheus reads. Grafana turns those series into dashboards and, later, alerts. Together they give you a clear view of CPU load, memory pressure, disk space and network traffic on a server, with history you can query after the fact.

Use this stack when you run one or more Linux servers and you want to see trends, catch disks that will fill up and compare load across periods. It is a common base for everything that comes after, including the Loki log stack in this series, which can use the same Grafana. Do not use it as the only check for whether a site is up from the outside, because Prometheus only sees what the host reports. Do not expose Node Exporter to the internet, since its output reveals a lot about the machine. Do not treat the default settings as capacity planning; retention and scrape intervals should match your disk size and the number of targets you add.

docker-compose.yml

services:
  node-exporter:
    image: prom/node-exporter:v1.8.2
    restart: unless-stopped
    command:
      - --path.rootfs=/host
    pid: host
    volumes:
      - /:/host:ro,rslave
    expose:
      - "9100"

  prometheus:
    image: prom/prometheus:v2.54.1
    restart: unless-stopped
    command:
      - --config.file=/etc/prometheus/prometheus.yml
      - --storage.tsdb.retention.time=${PROMETHEUS_RETENTION}
    ports:
      - "127.0.0.1:${PROMETHEUS_PORT}:9090"
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - prometheus_data:/prometheus
    depends_on:
      node-exporter:
        condition: service_started
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://localhost:9090/-/healthy"]
      interval: 30s
      timeout: 5s
      retries: 3

  grafana:
    image: grafana/grafana:11.2.0
    restart: unless-stopped
    environment:
      GF_SECURITY_ADMIN_USER: ${GRAFANA_ADMIN_USER}
      GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_ADMIN_PASSWORD}
      GF_USERS_ALLOW_SIGN_UP: "false"
    ports:
      - "127.0.0.1:${GRAFANA_PORT}:3000"
    volumes:
      - grafana_data:/var/lib/grafana
    depends_on:
      prometheus:
        condition: service_healthy

volumes:
  prometheus_data:
  grafana_data:

.env

.env
# Prometheus web and API port, bound to 127.0.0.1
PROMETHEUS_PORT=9090

# How long Prometheus keeps metrics, for example 15d or 30d
PROMETHEUS_RETENTION=15d

# Grafana web port, bound to 127.0.0.1
GRAFANA_PORT=3000

# Grafana admin login. The password is applied on the first start only.
GRAFANA_ADMIN_USER=admin
GRAFANA_ADMIN_PASSWORD=CHANGE_ME_long_random_password

Start and verify

Shell
# 1. Create the Prometheus configuration next to docker-compose.yml
cat > prometheus.yml <<'EOF'
global:
  scrape_interval: 15s
  evaluation_interval: 15s

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

# 2. Validate, start and check the containers
docker compose config --quiet
docker compose up -d
docker compose ps
docker compose logs --tail=30 prometheus

# 3. Verify Prometheus sees both targets as up
curl -s http://127.0.0.1:9090/api/v1/targets | grep -o '"health":"[a-z]*"'

# 4. Verify Grafana is running
curl -s http://127.0.0.1:3000/api/health

Then open Grafana through an SSH tunnel, add a Prometheus data source with the URL http://prometheus:9090, and import a Node Exporter dashboard from the Grafana catalog.

Production checklist

  • Keep Prometheus and Grafana bound to 127.0.0.1 and reach them through an SSH tunnel or an HTTPS reverse proxy with authentication.
  • Node Exporter runs with pid: host and a read-only mount of the host root. That is broad access, so keep it on the internal network only (expose, not ports), and never publish port 9100.
  • Keep GF_USERS_ALLOW_SIGN_UP set to false and use a long password for the admin account.
  • Set PROMETHEUS_RETENTION from your disk size. Check the disk growth for a week before you choose the value.
  • Add an alert on low disk space, for example when node_filesystem_avail_bytes drops below a threshold, so you learn about a full disk before the server fails.
  • Back up prometheus_data and grafana_data. Dashboards and users live in the Grafana database inside grafana_data.
  • Pin each image to a version and upgrade Prometheus and Grafana one at a time, because configuration fields change between major releases.
  • Plan alerting with Alertmanager or Grafana alerts. Dashboards alone do not notify you.

Common problems

  • Symptom: The node target shows DOWN in Prometheus. Cause: The target name is wrong, or Node Exporter is not on the same Docker network. Fix: Run docker compose exec prometheus wget -qO- http://node-exporter:9100/metrics | head and check that metrics are returned.
  • Symptom: Prometheus reports permission denied on its data directory. Cause: The data path was changed to a host folder owned by another user. Fix: Use the named volume prometheus_data, or set the owner of the folder to the user the image runs as.
  • Symptom: Grafana says the Prometheus data source is not reachable. Cause: The data source URL uses localhost. Fix: Set the URL to http://prometheus:9090.
  • Symptom: Grafana login fails after you changed GRAFANA_ADMIN_PASSWORD in .env. Cause: The admin password is written when the database is created, and it is not updated on later starts. Fix: Run docker compose exec grafana grafana cli admin reset-admin-password 'NEW_PASSWORD'.
  • Symptom: The disk fills up over time. Cause: Retention is too long, or a target exposes a very large number of series. Fix: Lower PROMETHEUS_RETENTION, remove the high-cardinality label, and restart the stack.

Variations

  • Container metrics: Add a cAdvisor service to collect per-container CPU and memory. It needs access to the Docker runtime files, so check its current image tag and its mounts before you add it.
  • Alertmanager: Add an Alertmanager container and point Prometheus at it with the alerting section of prometheus.yml, so alerts can reach email or chat.
  • Logs in the same Grafana: Add the Loki stack from this series and attach it as a second data source, so metrics and logs share one dashboard.

Questions

What does each container do?

Node Exporter reads CPU, memory, disk and network data from the host. Prometheus scrapes that data every 15 seconds and stores it. Grafana draws dashboards from the Prometheus data.

Why does Grafana say the data source is not reachable?

Inside the Grafana container, localhost points to Grafana itself. Use http://prometheus:9090 as the URL of the Prometheus data source.

Can I change the Grafana admin password after the first start?

Changing GRAFANA_ADMIN_PASSWORD later has no effect on an existing database. Change the password in the Grafana UI, or reset it with grafana cli admin reset-admin-password.

How much disk does Prometheus use?

It depends on the number of series and the scrape interval. Set PROMETHEUS_RETENTION to the period you need, and watch the disk with an alert before it fills.

Adapt this stack

Load these files into the builder to rename services, change ports, add a cache or a proxy, and generate fresh passwords for the .env.

Open in builder
  • Loki, Promtail and Grafana Log Stack

    Collect Linux host logs with Loki and Promtail in Docker Compose and query them in Grafana. Pinned images, persistent volumes and a localhost-only Grafana port.

    • loki
    • promtail
    • grafana
    Difficulty: Intermediate
  • Caddy 2 Reverse Proxy + Static Web Server

    Caddy 2 in Docker Compose: serve a static site, proxy /api to a backend and get automatic HTTPS on a real domain. Pinned images and .env configuration.

    • caddy
    • whoami
    Difficulty: Beginner
  • Gitea Git Server with PostgreSQL 16

    Self-hosted Gitea with PostgreSQL 16 in Docker Compose: web Git hosting, SSH on port 2222, a healthcheck-gated database start and persistent volumes.

    • gitea
    • db
    Difficulty: Intermediate
  • Portainer CE Web UI for Docker Management

    Run Portainer CE in Docker Compose as a web UI for containers, images and volumes. Bound to localhost on HTTPS 9443, with persistent data and a pinned image.

    • portainer
    Difficulty: Intermediate

Browse more Infrastructure & Monitoring stacks or the full library.