Cloud VPS from $1.98/mo · pay with Bitcoin, Ethereum, Monero, USDT or Solana · paid in crypto
DevOps

Prometheus and Grafana with Docker Compose on one server

Build a Prometheus, node_exporter, Alertmanager and Grafana stack with Docker Compose, provision Grafana, write alert rules and scrape a second server safely.

CloudSat teamNovember 18, 20268 min read
Four connected glowing modules on a dark grid, linked by thin lines that end in a rising chart
On this pageWhy every service uses the host network

This walkthrough builds a complete metrics stack on one Linux server: Prometheus to collect and store, node_exporter to describe the host, Alertmanager to send email, and Grafana to draw it all, with the data source provisioned from a file. Every component listens on 127.0.0.1 only, and Caddy publishes Grafana over HTTPS. At the end you add a second server as a scrape target without exposing its metrics to the internet.

Versions are pinned to the current releases as of November 2026: Prometheus 3.16.0, node_exporter 1.12.1, Alertmanager 0.35.0 and Grafana 13.3.1. Bump them deliberately after reading the release notes. If you want the background first, Prometheus vs Grafana explains what each piece does.

Why every service uses the host network

node_exporter has to see the host's real network interfaces and file systems, so its documentation runs it with the host network and the host PID namespace. Putting the other three services on the host network too keeps the design simple: they talk over 127.0.0.1, nothing is published through Docker's port mapping, and so nothing can slip past the host firewall. Only Caddy faces the internet.

Lay out the files

/opt/monitoring
├── compose.yaml
├── prometheus/
│   ├── prometheus.yml
│   └── rules/node.yml
├── alertmanager/
│   ├── alertmanager.yml
│   └── smtp_password
└── grafana/provisioning/datasources/prometheus.yml

The Compose file

services:
  prometheus:
    image: prom/prometheus:v3.16.0
    network_mode: host
    restart: unless-stopped
    command:
      - --config.file=/etc/prometheus/prometheus.yml
      - --storage.tsdb.path=/prometheus
      - --storage.tsdb.retention.time=30d
      - --storage.tsdb.retention.size=20GB
      - --web.listen-address=127.0.0.1:9090
      - --web.enable-lifecycle
    volumes:
      - ./prometheus:/etc/prometheus:ro
      - prometheus-data:/prometheus

  node-exporter:
    image: quay.io/prometheus/node-exporter:v1.12.1
    network_mode: host
    pid: host
    restart: unless-stopped
    command:
      - --path.rootfs=/host
      - --web.listen-address=127.0.0.1:9100
    volumes:
      - /:/host:ro,rslave

  alertmanager:
    image: prom/alertmanager:v0.35.0
    network_mode: host
    restart: unless-stopped
    command:
      - --config.file=/etc/alertmanager/alertmanager.yml
      - --storage.path=/alertmanager
      - --web.listen-address=127.0.0.1:9093
      - --cluster.listen-address=
    volumes:
      - ./alertmanager:/etc/alertmanager:ro
      - alertmanager-data:/alertmanager

  grafana:
    image: grafana/grafana:13.3.1
    network_mode: host
    restart: unless-stopped
    environment:
      GF_SERVER_HTTP_ADDR: 127.0.0.1
      GF_SERVER_ROOT_URL: https://grafana.example.com
      GF_USERS_ALLOW_SIGN_UP: "false"
    volumes:
      - ./grafana/provisioning:/etc/grafana/provisioning:ro
      - grafana-data:/var/lib/grafana

volumes:
  prometheus-data:
  alertmanager-data:
  grafana-data:

Two lines deserve a comment. --web.enable-lifecycle lets you reload Prometheus after a config change without a restart. The empty --cluster.listen-address= turns off Alertmanager's clustering, which otherwise listens on port 9094 on every interface, a port a single server does not need.

Configure Prometheus

prometheus/prometheus.yml scrapes Prometheus itself, the local exporter and Alertmanager, and loads rules from a folder:

global:
  scrape_interval: 15s
  evaluation_interval: 15s

alerting:
  alertmanagers:
    - static_configs:
        - targets: ["127.0.0.1:9093"]

rule_files:
  - /etc/prometheus/rules/*.yml

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ["127.0.0.1:9090"]
  - job_name: alertmanager
    static_configs:
      - targets: ["127.0.0.1:9093"]
  - job_name: node
    static_configs:
      - targets: ["127.0.0.1:9100"]
        labels:
          instance: monitor-01

Setting the instance label by hand replaces the address with a readable name, which makes dashboards and alerts easier to read. Then a first set of rules in prometheus/rules/node.yml:

groups:
  - name: node
    rules:
      - alert: TargetDown
        expr: up == 0
        for: 2m
        labels:
          severity: critical
        annotations:
          summary: "{{ $labels.instance }} ({{ $labels.job }}) is not answering scrapes"
      - alert: DiskAlmostFull
        expr: node_filesystem_avail_bytes{fstype!~"tmpfs|overlay"} / node_filesystem_size_bytes{fstype!~"tmpfs|overlay"} < 0.10
        for: 15m
        labels:
          severity: warning
        annotations:
          summary: "{{ $labels.instance }}: less than 10% free on {{ $labels.mountpoint }}"
      - alert: MemoryPressure
        expr: node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes < 0.10
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: "{{ $labels.instance }}: less than 10% memory available"

The for clause is what keeps these alerts calm: a condition must hold for the whole period before anything fires.

Send alerts by email

Alertmanager groups alerts, waits briefly so related ones arrive together, and repeats unresolved ones on a schedule. alertmanager/alertmanager.yml:

global:
  smtp_smarthost: "smtp.example.com:587"
  smtp_from: "alerts@example.com"
  smtp_auth_username: "alerts@example.com"
  smtp_auth_password_file: /etc/alertmanager/smtp_password

route:
  receiver: email
  group_by: [alertname, instance]
  repeat_interval: 4h

receivers:
  - name: email
    email_configs:
      - to: "oncall@example.com"
        send_resolved: true

The password lives in its own file. The Prometheus project's images run as the nobody user, so give that user the file and nobody else:

cd /opt/monitoring
read -rsp 'SMTP password: ' pw && printf '%s' "$pw" > alertmanager/smtp_password && unset pw
chown 65534:65534 alertmanager/smtp_password
chmod 400 alertmanager/smtp_password

Alertmanager also supports chat and paging services as receivers, so the same routes can send critical alerts somewhere louder than an inbox.

Provision the Grafana data source

grafana/provisioning/datasources/prometheus.yml connects Grafana to Prometheus on startup, so a rebuilt server needs no clicking:

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

Start and check

cd /opt/monitoring
docker compose run --rm --entrypoint promtool prometheus check config /etc/prometheus/prometheus.yml
docker compose run --rm --entrypoint amtool alertmanager check-config /etc/alertmanager/alertmanager.yml
docker compose up -d
curl -s http://127.0.0.1:9090/-/ready
curl -s http://127.0.0.1:9090/api/v1/targets | jq -r '.data.activeTargets[] | "\(.labels.job) \(.labels.instance) \(.health)"'

Every target should report up. Before Grafana goes public, open it through a tunnel from your own machine with ssh -L 3000:127.0.0.1:3000 root@203.0.113.5, browse to http://localhost:3000, sign in as admin with the password admin, and set a real password at the prompt.

Test the alert path

An alert route you have never exercised is a guess. Break something on purpose and follow the alert through each stage:

docker compose stop node-exporter
sleep 150
curl -s http://127.0.0.1:9090/api/v1/alerts | jq -r '.data.alerts[] | "\(.labels.alertname) \(.labels.instance) \(.state)"'
docker compose start node-exporter

TargetDown turns pending at the first failed scrape and firing once its two-minute for period has passed. Alertmanager then holds the alert for its group wait, 30 seconds by default, so that related alerts share one message, and sends the email. When the exporter comes back, a second email reports the alert as resolved, because send_resolved is on.

If nothing arrives, read docker compose logs alertmanager: rejected logins and connection timeouts show up there. Send through an SMTP relay on port 587 rather than port 25, which is frequently blocked on cloud servers, and check the spam folder before changing anything else.

Publish Grafana with Caddy

Install Caddy on the host, point a DNS record at the server, and use a two-line site block. Caddy obtains and renews the certificate itself and proxies Grafana's WebSocket connections without extra settings:

grafana.example.com {
    reverse_proxy 127.0.0.1:3000
}

Reload with systemctl reload caddy, then import a community dashboard for node_exporter metrics from Grafana's dashboard library to get a host overview in a minute.

Queries worth saving

PromQL works with rates and ratios more than raw values. These four expressions cover most host dashboards and make good first panels:

# CPU busy, percent, per server
100 * (1 - avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])))

# memory in use, percent
100 * (1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)

# free space on the root file system, percent
100 * node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"}

# inbound network traffic in bits per second, loopback excluded
8 * rate(node_network_receive_bytes_total{device!="lo"}[5m])

rate() turns a counter that only ever grows into a per-second value averaged over a window. Make the window at least four scrape intervals long, one minute or more with this configuration, so that one missed scrape does not punch holes in the graph. Try each expression on the Prometheus query page through your SSH tunnel first, then paste it into a Grafana panel. When a dashboard gets slow, turn its heaviest expressions into recording rules, which Prometheus evaluates once per interval instead of on every page load.

Add a second server

On the server you want to watch, run node_exporter the same way, but listening on an address the monitoring server can reach, such as its public IP or a WireGuard address. Then allow port 9100 from the monitoring server only:

# on web-01: let only the monitoring server (203.0.113.5) reach the exporter
sudo ufw allow from 203.0.113.5 to any port 9100 proto tcp

On CloudSat, add the same rule to the server's cloud firewall so the port stays closed to everyone else before traffic even reaches the host. Add the target to the static_configs list of the node job in prometheus.yml, then reload:

      - targets: ["198.51.100.20:9100"]
        labels:
          instance: web-01
curl -X POST http://127.0.0.1:9090/-/reload

Metrics cross the network in clear text this way. Over the public internet, prefer a WireGuard tunnel, or give the exporter TLS and basic authentication with its --web.config.file option.

Keep it running

  • Upgrades: change the image tags, run docker compose pull && docker compose up -d, and check the targets page again.
  • Disk: the retention size cap protects the disk, but alert on it anyway; the DiskAlmostFull rule above covers the monitoring server too.
  • Backups: the grafana-data volume holds users, dashboards you built by hand and alert state. Metrics in prometheus-data can be rebuilt only going forward, so decide whether they are worth a backup.
  • Watch the watcher: if this server dies, nothing alerts. Point an external uptime check, such as Uptime Kuma on another server, at Grafana's URL.

FAQ

Can I monitor containers as well as the host?

Yes. cAdvisor exports per-container CPU, memory and network metrics in Prometheus format; add it as another service and scrape job. Many applications also expose their own /metrics endpoint, which you scrape the same way.

How much disk will Prometheus use?

It depends on how many series you collect and how often. Check prometheus_tsdb_head_series after a day of running, then apply the capacity formula from the Prometheus and Grafana explainer. Whatever the estimate says, the 20 GB size cap in the Compose file keeps the database from filling the disk.

How do I reset a forgotten Grafana admin password?

Run docker compose exec grafana grafana cli admin reset-admin-password 'new-password', then sign in and change it from your profile.

Why not publish ports and use a Docker network instead?

You can, but every published port then needs care, because Docker's rules can bypass a host firewall such as UFW. With host networking and 127.0.0.1 listeners, the only service reachable from outside is the one you chose: Caddy.

Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.