Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js + Prometheus - Target Down Connection Refused

I am running a node applications locally. It runs on http://localhost:3002 using prom-client i can see the metrics at the following endpoint http://localhost:3002/metrics.

I've setup prometheus in a docker container and ran it.

Dockerfile

FROM prom/prometheus
ADD prometheus.yml /etc/prometheus/

prometheus.yml

scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:3002']
        labels:
          service: 'my-service'
          group: 'production'
rule_files:
  - 'alert.rules'

docker build -t my-prometheus .
docker run -p 9090:9090 my-prometheus

When i navigate to http://localhost:9090/targets it shows

Get http://localhost:3002/metrics: dial tcp 127.0.0.1:3002: connect: connection refused

enter image description here

Can you please tell me what im doing wrong here. node app is running on localhost at that port becasue when i go to http://localhost:3002/metrics i can see the metrics.

like image 575
Kay Avatar asked May 06 '26 11:05

Kay


1 Answers

When you are inside a container, you cannot access the localhost directly. You will need to add docker.for.mac.localhost to your prometheus.yml file. See below:

Your Job in prometheus.yml file. - job_name: 'prometheus'

# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.

static_configs:
- targets: ['localhost:9090']
- targets: ['docker.for.mac.localhost:3002']  
like image 128
Brandon Gilzean Avatar answered May 10 '26 18:05

Brandon Gilzean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!