Prometheus Docker
This guide outlines the steps to set up Prometheus and Node Exporter using Docker containers. It also addresses a common issue where the Prometheus container is unable to connect to the Node Exporter container, and how to resolve it by creating a Docker network.
Prerequisites
- Docker installed on the host machine.
Steps
1. Create a Docker Network
Creating a Docker network will ensure that the Prometheus and Node Exporter containers can communicate with each other.
docker network create prometheus-net
2. Run Node Exporter Container
Run the Node Exporter container in the created network. This will expose Node Exporter on port 9100.
docker run --name node_exporter --network=prometheus-net -d -p 9100:9100 prom/node-exporter
3. Configure Prometheus
Before running the Prometheus container, make sure to configure it correctly. Create a prometheus.yml file with the following content:
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node_exporter'
static_configs:
- targets: ['node_exporter:9100']
The targets under node_exporter should point to the name of the Node Exporter container and the port it's running on, which in this case is node_exporter:9100.
4. Run Prometheus Container
Run the Prometheus container in the same network as Node Exporter. This will expose Prometheus on port 9090.
docker run --name prometheus --network=prometheus-net -d -p 9090:9090 -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
Replace /path/to/prometheus.yml with the actual path to your prometheus.yml configuration file.
5. Verify the Setup
Open your web browser and navigate to http://localhost:9090. Click on Targets under the Status menu. You should see that the Node Exporter target is UP.
Troubleshooting
If you encounter a connection refused error, double-check the following:
- Both containers are running in the same Docker network.
- The
prometheus.ymlconfiguration file has the correcttargetsfor Node Exporter. - Ports 9090 and 9100 are open and not being used by other services.