Prometheus and Docker Compose

How to setup prometheus for local development

1. Create a docker-compose.yml file

INFO

If your application is running on your development machine natively, you must use the network_mode: host to access the docker container on the host machine.

docker-compose.yml
1version: '3.7'
2services:
3  prometheus:
4    image: prom/prometheus:latest
5    restart: unless-stopped
6    network_mode: host # use host network to access the host machine
7    ports:
8      - '9090:9090'
9    volumes:
10      - ./prometheus.yml:/etc/prometheus/prometheus.yml

2. Create a prometheus.yml file

prometheus.yml
1global:
2  scrape_interval: 5s
3
4  # Attach these labels to any time series or alerts when communicating with
5  # external systems (federation, remote storage, Alertmanager).
6  external_labels:
7    monitor: 'app'
8
9# A scrape configuration containing exactly one endpoint to scrape:
10# Here it's Prometheus itself.
11scrape_configs:
12  - job_name: 'backend'
13    static_configs:
14      - targets: ['localhost:3001'] # Metrics endpoint of the frontend application
15        labels:
16          group: 'app'
INFO

If you want to see a real world example, check out the demo repo.

Screenshots

Shows a prometheus query
Shows the prometheus targets