Home > Net >  How to run prometheus on docker-compose and scrape django server running locally?
How to run prometheus on docker-compose and scrape django server running locally?

Time:10-14

I am trying to setup prometheus to monitor my Django application using django-prometheus and Docker compose. I've been following some guides online but different from all the guides I've seen, I want to run Django locally for now so simply python manage.py runserver and run prometheus with docker-compose (and later add grafana). I want to do this to test it locally and later I will deploy it to Kubernetes but this is for another episode.

My issue is to make the local running django server communicate in the same network as the prometheus running container because I get this error in the /targets on prometheus dashboard:

Get "http://127.0.0.1:5000/metrics": dial tcp 127.0.0.1:5000: connect: connection refused

These are my docker-compose file and prometheus configuration:

  • docker-compose.yml
version: '3.6'

services:
  prometheus:
    image: prom/prometheus
    volumes:
    - ./prometheus/:/etc/prometheus/
    command:
    - '--config.file=/etc/prometheus/prometheus.yml'
    ports:
    - 9090:9090
  • prometheus.yaml
global:
  scrape_interval: 15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets:
        - localhost:9090

  - job_name: django-app
    static_configs:
      - targets:
        - localhost:8000
        - 127.0.0.1:8000

CodePudding user response:

If you want to run the Django app outside of (a container and outside of) Docker Compose then, when run, it will bind to one of the host's ports.

You need to get the Docker Compose prometheus service to bind to the host's network too.

You should be able to do this using network_mode: host under the prometheus service.

Then, prometheus will be able to access the Django app on the host port that it's using and prometheus will be accessible as localhost:9090 (without needing the ports section).

  • Related