DEV Community

Cover image for Building a Complete Monitoring Stack with Prometheus and Grafana using Docker
Durrell  Gemuh
Durrell Gemuh

Posted on

Building a Complete Monitoring Stack with Prometheus and Grafana using Docker

Monitoring your infrastructure shouldn't be complicated. In this guide, I'll show you how to spin up a complete monitoring solution with Prometheus and Grafana in just a few commands using Docker Compose.

What You'll Get

By the end of this tutorial, you'll have:

  • Prometheus for metrics collection and storage
  • Grafana for beautiful dashboards and visualization
  • Node Exporter for system metrics (CPU, memory, disk)
  • Nginx with its own exporter for web server monitoring
  • Everything containerized and easy to manage

Prerequisites

You'll need:

  • Docker and Docker Compose installed
  • Basic understanding of containers
  • 5 minutes of your time

Architecture Overview

Our stack consists of 5 services working together:

┌─────────────┐
│   Grafana   │ (Port 3000)
│  Dashboard  │
└──────┬──────┘
       │ Queries
       ↓
┌─────────────┐      ┌──────────────┐
│ Prometheus  │◄─────┤ Node Exporter│ (System Metrics)
│   (Port     │      └──────────────┘
│    9090)    │
└──────┬──────┘      ┌──────────────┐
       └─────────────┤Nginx Exporter│ (Web Metrics)
                     └──────┬───────┘
                            ↓
                     ┌──────────────┐
                     │    Nginx     │ (Port 8080)
                     └──────────────┘
Enter fullscreen mode Exit fullscreen mode

Quick Start

1. Clone the Repository

https://github.com/durrello/prometheus-grafana-docker.git
cd prometheus-grafana-docker
Enter fullscreen mode Exit fullscreen mode

2. Start Everything

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

That's it! Your monitoring stack is now running.

Accessing Your Services

Once everything is up, you can access:

Service URL Credentials
Grafana http://localhost:3000 admin / admin
Prometheus http://localhost:9090 -
Node Exporter http://localhost:9100/metrics -
Nginx http://localhost:8080 -
Nginx Exporter http://localhost:9113/metrics -

Setting Up Your First Dashboard

Step 1: Add Prometheus as a Data Source

  1. Log into Grafana (http://localhost:3000)
  2. Go to ConfigurationData Sources
  3. Click Add data source
  4. Select Prometheus
  5. Set URL to http://prometheus:9090
  6. Click Save & Test

Step 2: Import a Dashboard

Grafana has thousands of pre-built dashboards. Here are some great ones to start with:

For Node Exporter (System Metrics):

  1. Click +Import
  2. Enter dashboard ID: 1860
  3. Select your Prometheus data source
  4. Click Import

For Nginx:

  1. Click +Import
  2. Enter dashboard ID: 12708
  3. Select your Prometheus data source
  4. Click Import

Understanding the Configuration

Docker Compose Services

Prometheus:

prometheus:
  image: prom/prometheus:latest
  volumes:
    - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
  ports:
    - "9090:9090"
Enter fullscreen mode Exit fullscreen mode

Grafana:

grafana:
  image: grafana/grafana:latest
  environment:
    - GF_SECURITY_ADMIN_USER=admin
    - GF_SECURITY_ADMIN_PASSWORD=admin
  ports:
    - "3000:3000"
  volumes:
    - grafana-data:/var/lib/grafana
Enter fullscreen mode Exit fullscreen mode

Prometheus Scrape Configuration

The prometheus/prometheus.yml file defines what metrics to collect:

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['prometheus:9090']

  - job_name: 'node_exporter'
    static_configs:
      - targets: ['nodeexporter:9100']

  - job_name: 'nginx'
    static_configs:
      - targets: ['nginxexporter:9113']
Enter fullscreen mode Exit fullscreen mode

Prometheus scrapes metrics every 15 seconds from these endpoints.

Alerting (Bonus)

The setup includes a sample alert configuration in prometheus/alerts.yml:

- alert: HighCPUUsage
  expr: node_cpu_seconds_total{mode="idle"} < 20
  for: 1m
  labels:
    severity: warning
  annotations:
    summary: "CPU usage is high"
Enter fullscreen mode Exit fullscreen mode

You can extend this with more sophisticated alerts based on your needs!

Common Commands

View logs:

docker-compose logs -f prometheus
docker-compose logs -f grafana
Enter fullscreen mode Exit fullscreen mode

Restart a service:

docker-compose restart prometheus
Enter fullscreen mode Exit fullscreen mode

Stop everything:

docker-compose down
Enter fullscreen mode Exit fullscreen mode

Stop and remove volumes (clean slate):

docker-compose down -v
Enter fullscreen mode Exit fullscreen mode

What's Next?

Now that you have a working monitoring stack, you can:

  1. Add more exporters - MySQL, PostgreSQL, Redis, etc.
  2. Create custom dashboards - Visualize metrics specific to your apps
  3. Set up alerting - Get notified via Slack, email, or PagerDuty
  4. Monitor custom applications - Instrument your code with Prometheus client libraries
  5. Scale it up - Deploy this to production with proper storage and HA setup

Key Takeaways

  • Docker Compose makes it trivial to run complex multi-service stacks
  • Prometheus is pull-based - it scrapes metrics from exporters
  • Grafana provides beautiful, flexible visualization
  • Exporters bridge the gap between services and Prometheus
  • This setup is production-ready with minimal tweaks (change default passwords!)

Resources


Have questions or suggestions? Drop them in the comments below! 👇

If you found this helpful, give it a ❤️ and share it with your team!

Top comments (0)