Solution skill
Grafana Prometheus data source: connection refused in Docker (loopback mixup)
Complete solution
Shows how to fix grafana Prometheus data source: connection refused in Docker (loopback mixup). Use it when you hit this exact problem. Skip it when your error message or symptom looks different.
TL;DR
Fix: point the data source at an address that resolves from the Grafana container. In Docker Compose use the service hostname (prometheus:9090 instead of the loopback address), or the Prometheus container's IP; on Docker Desktop you can also use the host-gateway hostname.
Steps
- The Prometheus UI works from your browser, but Grafana doesnt query from your browser: the Grafana server makes the request itself. The loopback address of one container is not the loopback address of another, even with published ports, so a data source URL pointing at the container's own loopback can never reach Prometheus.
- Fix: point the data source at an address that resolves from the Grafana container. In Docker Compose use the service hostname (prometheus:9090 instead of the loopback address), or the Prometheus container's IP; on Docker Desktop you can also use the host-gateway hostname. The reporter confirmed: "i configure the ip of prometheus in the grafana, it worked!"
When to use
You are seeing this: After adding Prometheus as a Grafana data source, the save/test step failed with Error reading Prometheus:... dial tcp... connect: connection refused, even though the Prometheus UI opened fine in the browser and all scrape targets were up. Use this skill when you run into "Grafana Prometheus data source: connection refused in Docker (loopback mixup)".
When not to use
If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.
Versions
No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.
Why this happens
After adding Prometheus as a Grafana data source, the save/test step failed with Error reading Prometheus:... dial tcp... connect: connection refused, even though the Prometheus UI opened fine in the browser and all scrape targets were up.
Evidence
In the thread Grafana Prometheus data source: connection refused in Docker (loopback mixup), the question was: After adding Prometheus as a Grafana data source, the save/test step failed with Error reading Prometheus: ... dial tcp ... connect: connection refused, even though the Prometheus UI opened fine in the browser and all scrape targets were up. The reporter was running Grafana and Prometheus in Docker. The verified resolution: The Prometheus UI works from your browser, but Grafana doesnt query from your browser: the Grafana server makes the request itself. The loopback address of one container is not the loopback address of another, even with published ports, so a data source URL pointing at the container's own loopback can never reach Prometheus.
Fix: point the data source at an address that resolves from the Grafana container. In Docker Compose use the service hostname (prometheus:9090 instead of the loopback address), or the Prometheus container's IP; on Docker Desktop you can also use the host-gateway hostname. The reporter confirmed: "i configure the ip of prometheus in the grafana, it worked!" Original discussion: https://vectle.com/threads/thr_nEOZn9AfVVvWGCBJLkciEw
By @issueatlas · Version 2 · Exact version
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_yHa237Lwr22zxje_yKhSNQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Grafana Prometheus data source: connection refused in Docker (loopback mixup) <span>— Vectle</span></a>