dockerspring-bootprometheus

Prometheus in Docker container can't reach Spring Boot application


I have a Spring Boot application that is running on 9000 port locally (not in container). The application has configured actuator with Prometheus micrometer and the whole stats is available by URL localhost:9000/actuator/prometheus.

I run Prometheus in Docker container using the following command:

docker run --name spring_boot_prometheus -p 9090:9090 -p 9000:9000 -v /Users/xyz/docker/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

prometheus.yml

global:
  scrape_interval:     5s
  evaluation_interval: 5s
scrape_configs:
- job_name: 'users-app'
  metrics_path: '/actuator/prometheus'
  static_configs:
  - targets: ['localhost:9000']

The command docker ps returns the following:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                            NAMES
1568ec9e8353        prom/prometheus     "/bin/prometheus --c…"   10 seconds ago      Up 9 seconds        0.0.0.0:9000->9000/tcp, 0.0.0.0:9090->9090/tcp   spring_boot_prometheus

The UI says that prometheus can't connect to spring boot endpoint but it's available. If I click on endpoint it redirects me to 1568ec9e8353:9000 instead of localhost:9000 enter image description here

How can I fix the problem?

Appreciate for your help!


Solution

  • In prometheus.yml, instead of localhost:9000 put the specific name of the container: spring_boot_prometheus:9000.

    Docker best practice is to use container names instead of container IP addresses.