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.
Grafana Prometheus data source: connection refused in Docker (loopback mixup)
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!"
Source: https://github.com/grafana/grafana/issues/46434
Source: https://github.com/grafana/grafana/issues/46434