Setting Up Prometheus with Docker Compose for Local Development

When developing applications, monitoring performance metrics is crucial for identifying bottlenecks, ensuring reliability, and optimizing resource usage. Prometheus is an open-source monitoring and alerting toolkit designed for this purpose. This blog post will guide you through setting up Prometheus for local development using Docker Compose.

Why Use Prometheus Locally?

Using Prometheus locally allows developers to:

  • Quickly identify performance issues in real-time.
  • Test alerts and monitoring configurations before deploying to production.
  • Understand how applications behave under different conditions or loads.

1. Create a docker-compose.yml file

INFO

When running your application on your development machine outside of Docker, use network_mode: host to ensure Docker containers can communicate with services on your host machine.

docker-compose.yml
version: '3.7' services: prometheus: image: prom/prometheus:latest restart: unless-stopped network_mode: host # use host network to access the host machine ports: - '9090:9090' volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml

2. Create a prometheus.yml file

Create a prometheus.yml file to define how Prometheus should scrape metrics:

prometheus.yml
global: scrape_interval: 5s # Attach these labels to any time series or alerts when communicating with # external systems (federation, remote storage, Alertmanager). external_labels: monitor: 'app' # A scrape configuration containing exactly one endpoint to scrape: # Here it's Prometheus itself. scrape_configs: - job_name: 'backend' static_configs: - targets: ['localhost:3001'] # Metrics endpoint of the frontend application labels: group: 'app'

Explanation:

  • scrape_interval - Defines how frequently Prometheus checks for metrics.
  • external_labels - Adds labels to all metrics for easy filtering.
  • scrape_configs - Defines which targets Prometheus should scrape metrics from.
INFO

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

Screenshots

Shows a prometheus query
Shows the prometheus targets