Understanding the Architecture and Network Model
To troubleshoot effectively, you must first understand how Docker handles networking by default. In a standard bridge network setup, containers are isolated from the host’s loopback interface (127.0.0.1). If Prometheus runs in one container and attempts to scrape Node Exporter on localhost, it will fail because localhost inside the Prometheus container refers to itself, not the host machine where Node Exporter might be running.
When using Docker Compose or standalone docker run commands, services communicate via container names or IP addresses within the same Docker network. If Node Exporter is installed directly on the host OS (not in a container), Prometheus needs to know the host's actual IP address on the Docker bridge network, typically accessible via host.docker.internal on macOS/Windows or the gateway IP on Linux. Misconfiguring this target URL is the most frequent cause of "connection refused" errors.
Additionally, ensure that both services are attached to the same Docker network. If they are on separate networks without explicit inter-network routing, packets will be dropped. You can verify this by checking the output of docker network ls and confirming both containers are listed under the same network ID.
Verifying Port Exposure and Bindings
Node Exporter listens on port 9100 by default. A critical configuration error occurs when the exporter binds only to127.0.0.1. In this case, it accepts connections only from the local machine and rejects external requests, including those from other containers. To fix this, ensure the Node Exporter container binds to 0.0.0.0:9100 or specifically to the Docker bridge interface.
In your docker-compose.yml, the ports directive maps the container port to the host. For example:
services:
node-exporter:
image: prom/node-exporter
ports:
- "9100:9100"
networks:
- monitoring
If you are running Node Exporter as a system daemon on the host rather than in a container, check its systemd service file or startup script. Ensure the --web.listen-address flag is set to :9100 or 0.0.0.0:9100. Binding to 127.0.0.1:9100 will prevent Prometheus from scraping it if they are not on the same host namespace. Use netstat -tuln | grep 9100 or ss -tuln | grep 9100 to verify which interfaces are listening.
For Docker environments, if you need Prometheus to access the host's Node Exporter, you may need to use network_mode: host for the Prometheus container or configure the extra_hosts directive to resolve the host IP correctly. However, the recommended practice is to run Node Exporter in a container within the same overlay network as Prometheus.
Diagnosing DNS and Service Discovery Issues
Docker uses an embedded DNS server to resolve container names. If Prometheus cannot resolve the hostname of the Node Exporter container, it will fail to connect. Test this by executingdocker exec prometheus wget -qO- http://node-exporter:9100/metrics. If this returns HTTP 403 or connection timeout, there is a DNS or network isolation issue.
Ensure that the Prometheus configuration file (prometheus.yml) uses the correct static configuration or service discovery mechanism. For static configs, the targets list should use the container name, not localhost:
scrape_configs:
- job_name: 'node-exporter'
static_configs:
- targets: ['node-exporter:9100']
If you are using Kubernetes or more complex orchestration, ensure that the Service object exposes port 9100 and that the Endpoints are populated. In pure Docker environments, verify that no iptables rules are blocking traffic between the containers. You can temporarily disable iptables for testing purposes to rule out firewall interference, but remember to re-enable it afterward.
Checking Firewall and Security Groups
Even within Docker, host-level firewalls can block traffic. If Node Exporter is running on the host, ensure that the host firewall (such asufw on Ubuntu or firewalld on CentOS) allows incoming TCP connections on port 9100. Run sudo ufw status to check active rules. If UFW is enabled, add a rule with sudo ufw allow 9100/tcp.
Similarly, if you are using cloud providers like AWS or Azure, security groups or network ACLs might restrict inbound traffic. Ensure that the instance running Prometheus or the host running Node Exporter has an inbound rule allowing TCP port 9100 from the relevant CIDR blocks.
Integrating with Grafana for Visualization
Once Prometheus successfully scrapes Node Exporter, you can visualize the data in Grafana. Add Prometheus as a data source in Grafana, pointing tohttp://prometheus:9090. Import a standard Node Exporter dashboard, such as Dashboard ID 1860, which provides comprehensive metrics on CPU, memory, disk I/O, and network usage.
If Grafana shows "No Data" despite Prometheus having metrics, check the time range selector in Grafana. Ensure that the time window covers the period when metrics were collected. Also, verify that the PromQL queries in the dashboard panels are valid and match the metric labels exposed by Node Exporter. Common label issues include missing instance or job labels due to misconfigured relabeling actions in Prometheus.
Final Verification Steps
Perform a final end-to-end test. From the Prometheus container, curl the Node Exporter endpoint. From the host, curl the Prometheus/api/v1/targets API endpoint to see if the Node Exporter target state is UP. If the state is DOWN or UNKNOWN, check the Prometheus logs for specific error messages like "context deadline exceeded" or "connection refused". These logs provide precise clues about whether the issue is network-related, authentication-based, or configuration-driven.
By systematically checking network models, port bindings, DNS resolution, and firewall rules, you can isolate and resolve the majority of connection issues between Prometheus and Node Exporter in Docker environments.
Frequently Asked Questions
Why does Prometheus show Node Exporter as DOWN?
This usually indicates that Prometheus cannot reach the target endpoint. Check if the target URL in prometheus.yml matches the container name or IP address exactly. Verify that Node Exporter is running and listening on port 9100 without binding restrictions.
How do I access host metrics if Node Exporter is not in Docker?
If Node Exporter runs on the host, use the special hostname host.docker.internal in Prometheus targets on macOS/Windows, or the Docker gateway IP on Linux. Alternatively, run Prometheus with network_mode: host to share the host network namespace.
Can I use localhost to connect Prometheus to Node Exporter?
No, unless both services run in the same container or use network_mode: host. In standard Docker setups, localhost refers to the container itself, so Prometheus would try to scrape itself instead of the exporter.
What if I get a 403 Forbidden error from Node Exporter?
A 403 error often means Node Exporter is configured to require authentication or has basic auth enabled. Ensure that Prometheus includes the correct credentials in the basic_auth section of the scrape config, or disable auth if security is not required.
How do I debug DNS resolution between Docker containers?
Use docker exec <container_name> ping <other_container_name> to test connectivity. If ping fails, check if both containers are on the same Docker network using docker network inspect <network_name>.