Prometheus Grafana Setup: 7 Easy Steps and Mistakes to Avoid
Prometheus collects and stores metrics, Grafana draws them. Prometheus and Grafana together are the most common open source monitoring stack on Linux, and a Prometheus Grafana setup with Docker takes only a few commands. The first version of this guide listed those commands but skipped the parts that actually break: the bind mount fails when prometheus.yml does not exist yet, alert rules do not go inside prometheus.yml, and the http://localhost:9090 data source URL does not work when Grafana runs in its own container. This rewrite builds the whole Prometheus Grafana stack and shows the real output of every step.
Lab note: everything below ran on 7 November 2026 with rootless podman (called as docker) on Fedora, using the official images: Prometheus 3.16.0, node_exporter 1.12.1 and Grafana 13.3.0. The Docker CLI takes the same options. Containers read the host's /proc, so CPU and memory values come from the lab machine.
Contents
- What each part of a Prometheus Grafana stack does
- Step 1: create prometheus.yml before the Prometheus Grafana containers
- Step 2: alert rules belong in rule_files, not prometheus.yml
- Step 3: run Prometheus and node_exporter on one network
- Step 4: Prometheus Grafana queries that actually answer a question
- Step 5: connect Grafana to Prometheus (and why localhost fails)
- Step 6: dashboards and provisioning instead of clicks
- Step 7: test the InstanceDown alert
- Prometheus Grafana security: Prometheus has no login by default
- Prometheus Grafana checklist and FAQ
- Conclusion
What each part of a Prometheus Grafana stack does
Before typing anything, it helps to know which part of Prometheus and Grafana owns which job, because most problems come from asking the wrong component:
| Component | Job | Port in this lab |
|---|---|---|
| node_exporter | exposes Linux host metrics (CPU, memory, disk, network) on /metrics | 9100 (network only) |
| Prometheus | scrapes targets every scrape_interval, stores time series, runs PromQL and alert rules | 9090 |
| Grafana | dashboards; sends PromQL to Prometheus through a data source | 3000 |
Grafana does not collect anything by itself. If a panel is empty, the question is always first "does Prometheus have this series?" and only then "is the Grafana query right?". The Prometheus overview describes the pull model in detail.
Step 1: create prometheus.yml before the Prometheus Grafana containers
The old post's command mounts $(pwd)/prometheus.yml. Run it in a folder without that file and it stops at once:
$ docker run -d -p 9090:9090 --name=prometheus -v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml docker.io/prom/prometheus:latest
Error: statfs /tmp/tmp.P9Mfa2KtxE/prometheus.yml: no such file or directory
[exit 125]
That is the podman message. With Docker Engine, the bind mount docs say a missing source path for --volume is "always created as a directory", which is worse because it fails later inside the container. Either way: write the file first. The Prometheus installation docs show the same bind mount of a file you already have.
This is the Prometheus configuration used for the rest of the Prometheus Grafana lab. It scrapes Prometheus itself and node_exporter, and loads alert rules from a separate file:
global:
scrape_interval: 15s
evaluation_interval: 15s
rule_files:
- /etc/prometheus/alerts.yml
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ["localhost:9090"]
- job_name: node
static_configs:
- targets: ["node-exporter:9100"]
Step 2: alert rules belong in rule_files, not prometheus.yml
The first version said "Prometheus Alerts: Configure rules in prometheus.yml". I put a rule group directly into prometheus.yml and asked promtool, which ships in the Prometheus image, to check it:
$ docker run --rm -v ./prometheus.yml:/p.yml:Z --entrypoint promtool docker.io/prom/prometheus:latest check config /p.yml
Checking /p.yml
FAILED: parsing YAML file /p.yml: yaml: unmarshal errors:
line 7: field groups not found in type config.plain
[exit 1]
Rules live in their own file, listed under rule_files. This alerts.yml has an "instance down" rule and a low memory rule:
groups:
- name: node
rules:
- alert: InstanceDown
expr: up == 0
for: 30s
labels:
severity: critical
annotations:
summary: "{{ $labels.job }} target {{ $labels.instance }} is down"
- alert: LowMemory
expr: node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes * 100 < 10
for: 5m
labels:
severity: warning
Run promtool check config before every restart. It follows rule_files and checks the rules too:
Checking /etc/prometheus/prometheus.yml
SUCCESS: 1 rule files found
SUCCESS: /etc/prometheus/prometheus.yml is valid prometheus config file syntax
Checking /etc/prometheus/alerts.yml
SUCCESS: 2 rules found
Step 3: run Prometheus and node_exporter on one network
Prometheus and Grafana containers find each other by name only on a user-defined network. Create one, start node_exporter, then Prometheus with both files mounted. Publishing on 127.0.0.1 keeps the ports off the LAN while you test:
docker network create mon
docker run -d --name node-exporter --network mon docker.io/prom/node-exporter:latest
docker run -d --name prometheus --network mon -p 127.0.0.1:9090:9090 \
-v ./prometheus.yml:/etc/prometheus/prometheus.yml:Z \
-v ./alerts.yml:/etc/prometheus/alerts.yml:Z \
docker.io/prom/prometheus:latest
(:Z is the SELinux relabel option; Fedora and RHEL need it, other hosts ignore it.) Check readiness and targets through the HTTP API:
$ curl -s localhost:9090/-/ready
Prometheus Server is Ready.
$ curl -s localhost:9090/api/v1/targets | jq -r '.data.activeTargets[] | "\(.labels.job) \(.scrapeUrl) \(.health)"'
node http://node-exporter:9100/metrics up
prometheus http://localhost:9090/metrics up
Both targets are up. If a target shows down, the lastError field of the same API answer tells you why (an example is in step 7).
Step 4: Prometheus Grafana queries that actually answer a question
The old post's CPU query is rate(node_cpu_seconds_total{mode="user"}[5m]). It is valid, but it returns one series per CPU core, which makes a messy panel:
$ query: rate(node_cpu_seconds_total{mode="user"}[5m]) (old post, series count)
20
Twenty lines for a 20-thread machine. For a single "how busy is this host" number, average the idle rate per instance and invert it:
$ query: CPU busy % per instance
node-exporter:9100 46.62631750000015
$ query: (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100
node-exporter:9100 42.00454123397495
The query was 100 * (1 - avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[1m]))). The memory query from the first version is correct and gives available memory in percent. Two mistakes are worth knowing because they behave very differently. A wrong metric name is not an error, it is just an empty result, so a panel stays blank without any warning:
$ query: wrong metric name node_cpu_seconds (no _total)
[]
$ query: syntax error
bad_data
invalid parameter "query": 1:32: parse error: unclosed left parenthesis
This is the most common Prometheus Grafana support question: when a Grafana panel shows "No data", paste the query into the Prometheus UI at :9090 first; an empty [] there means the name or labels are wrong.
Step 5: connect Grafana to Prometheus (and why localhost fails)
Start Grafana on the same network and check its health endpoint. The first login is admin / admin, and per the sign-in docs Grafana then prompts you to change the password:
docker run -d --name grafana --network mon -p 127.0.0.1:3000:3000 docker.io/grafana/grafana:latest
$ curl -s localhost:3000/api/health
{
"database": "ok",
"version": "13.3.0",
"commit": "6f8ce3c693b493043ebcd4787566d2b6b118b0f0"
}
In Grafana 13 the menu is no longer "Configuration > Data Sources" as the old post said: the Grafana Prometheus data source docs say click Connections, then Add new connection, search for Prometheus and add it. The same docs suggest http://localhost:9090 "if Prometheus is running locally". In a containerized Prometheus Grafana setup that is the trap. I added a data source with that URL through the HTTP API, which does the same as the form, and asked Grafana to test it:
$ curl -s -u admin:admin localhost:3000/api/datasources/uid/promlocal/health | jq .
{
"message": "Post \"http://localhost:9090/api/v1/query\": dial tcp [::1]:9090: connect: connection refused - There was an error returned querying the Prometheus API.",
"status": "ERROR"
}
Inside the Grafana container, localhost is Grafana itself. Use the container name on the shared network instead:
$ curl -s -u admin:admin localhost:3000/api/datasources/uid/prom/health | jq .
{
"details": {
"application": "Prometheus",
"features": {
"rulerApiEnabled": false
}
},
"message": "Successfully queried the Prometheus API.",
"status": "OK"
}

If Grafana runs on the host and Prometheus in a container, localhost:9090 works again, because then the published port is on the host. Pick the URL from where Grafana runs, not where you sit: this one detail breaks more Prometheus and Grafana installs than anything else.
Step 6: dashboards and provisioning instead of clicks
A Prometheus Grafana dashboard is JSON. You can build it in the UI, or post it to the API. This one has a CPU time series and a memory gauge using the two queries from step 4:
$ curl -s -u admin:admin -H Content-Type:application/json -X POST localhost:3000/api/dashboards/db -d @dash.json | jq -c '{status, uid, url}'
{"status":"success","uid":"node-basics","url":"/d/node-basics/node-basics"}
Data sources clicked together in the UI are lost when the Grafana container is recreated without a volume. A provisioning file avoids that. Mount it into /etc/grafana/provisioning/datasources:
apiVersion: 1
datasources:
- name: Prometheus-prov
uid: promprov
type: prometheus
access: proxy
url: http://prometheus:9090
After recreating the container with -v ./prov/datasources:/etc/grafana/provisioning/datasources:Z, only the provisioned data source exists, and it is read only in the UI:
$ curl -s -u admin:admin -H Content-Type:application/json localhost:3000/api/datasources | jq -r '.[] | "\(.name) \(.url) readOnly=\(.readOnly)"'
Prometheus-prov http://prometheus:9090 readOnly=true
$ curl -s -u admin:admin -H Content-Type:application/json localhost:3000/api/datasources/uid/promprov/health | jq -r .status
OK
Notice what disappeared: the two data sources and the dashboard made through the API, because the first container had no volume for /var/lib/grafana. For a real Prometheus Grafana server, mount a named volume there and keep data sources in provisioning files under version control.
Step 7: test the InstanceDown alert
An alert rule you never saw fire is a guess, in Prometheus and Grafana alike. Stop the exporter and watch Prometheus. Per the alerting rules docs, an alert with a for clause stays pending until the condition has held for that long. About a minute after the stop:
$ curl -s localhost:9090/api/v1/alerts | jq -r '.data.alerts[] | "\(.labels.alertname) \(.labels.instance) \(.state) - \(.annotations.summary)"'
InstanceDown node-exporter:9100 firing - node target node-exporter:9100 is down
$ curl -s localhost:9090/api/v1/targets | jq -r '.data.activeTargets[] | select(.labels.job=="node") | "\(.health) \(.lastError)"'
down Get "http://node-exporter:9100/metrics": dial tcp: lookup node-exporter on 10.89.1.1:53: no such host
The lastError text is the fastest way to tell a DNS problem (wrong name or network, as here after the container stopped) from a refused port or a timeout. Prometheus only evaluates rules; sending email or chat messages needs Alertmanager (or Grafana Alerting), which this lab did not set up.
Prometheus Grafana security: Prometheus has no login by default
Grafana refuses a wrong password (401 from -u admin:wrong in the lab), but Prometheus answers anyone who can reach port 9090, including the full configuration:
$ curl -s -o /dev/null -w '%{http_code}\n' localhost:9090/api/v1/status/config
200
$ docker port prometheus; docker port grafana
9090/tcp -> 127.0.0.1:9090
3000/tcp -> 127.0.0.1:3000
So bind Prometheus to localhost or a private network on every Prometheus Grafana host, put a reverse proxy with TLS and authentication in front of anything public, change the Grafana admin password on first login, and do not publish node_exporter at all; Prometheus reaches it over the container network.
Prometheus Grafana checklist and FAQ
- Empty panel? Run the query in Prometheus first.
[]means wrong metric name or labels, not a Grafana bug. - Data source "connection refused"?
localhostinside a container is the container. Use the service name on a shared network. - Config change?
promtool check configbefore restarting. - Do Prometheus and Grafana need each other? No. Can I use Grafana without Prometheus? Yes, Grafana has many data source types; Prometheus is only one of them.
- How do I scale? Federation and remote storage projects such as Thanos exist for that; this lab covers a single server.
If Prometheus must scrape something that only listens on a Unix socket, see Prometheus Unix socket scraping. For the podman commands used here, see Podman vs Docker key differences.
Conclusion
A working Prometheus Grafana stack is three containers on one network, a prometheus.yml that exists before you mount it, rules in their own file checked by promtool, and a Grafana data source that points to the Prometheus container name instead of localhost. Test every alert by breaking the target once, keep data sources in provisioning files, and never leave port 9090 open to the internet.
Comments
Post a Comment