DEV Community

Abhay Singh Kathayat
Abhay Singh Kathayat

Posted on

Docker with Prometheus and Grafana: Real-Time Monitoring Simplified

Docker with Prometheus and Grafana: Monitoring Made Easy

Prometheus and Grafana are powerful tools for monitoring containerized applications and infrastructure. Docker simplifies deploying and managing these tools, enabling real-time monitoring, metrics visualization, and alerting.


Overview

  • Prometheus: An open-source monitoring system that collects metrics from applications and infrastructure. It uses a pull-based mechanism and stores time-series data.
  • Grafana: A visualization tool that creates interactive and customizable dashboards using data from Prometheus and other sources.

Why Use Prometheus and Grafana with Docker?

  1. Easy Setup: Docker provides prebuilt images for Prometheus and Grafana, reducing setup complexity.
  2. Portability: Run Prometheus and Grafana in isolated containers, ensuring consistency across environments.
  3. Scalability: Easily scale monitoring setups by deploying containers on multiple hosts using Docker Compose or orchestrators like Kubernetes.
  4. Integration: Monitor other Docker containers, services, and infrastructure components.

Setting Up Prometheus and Grafana with Docker

1. Prerequisites

  • Install Docker and Docker Compose on your system.
  • Basic understanding of Docker and container networking.

2. Define a Docker Compose File

Create a docker-compose.yml file to configure Prometheus and Grafana.

version: '3.8'

services:
  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    ports:
      - "9090:9090"

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    ports:
      - "3000:3000"
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=admin
Enter fullscreen mode Exit fullscreen mode

3. Configure Prometheus

Create a prometheus.yml file to define the Prometheus configuration.

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'docker'
    static_configs:
      - targets: ['host.docker.internal:9323']
Enter fullscreen mode Exit fullscreen mode

In this example:

  • Prometheus scrapes metrics from the Docker host using the Docker Daemon's metrics endpoint (via port 9323).

4. Run the Stack

Run the Prometheus and Grafana stack using Docker Compose:

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

This will start:

  • Prometheus on http://localhost:9090
  • Grafana on http://localhost:3000

5. Add Data Source in Grafana

  1. Access Grafana at http://localhost:3000 (default username/password: admin/admin).
  2. Navigate to Configuration > Data Sources.
  3. Add Prometheus as a data source:
    • URL: http://host.docker.internal:9090
    • Save and Test.

6. Import a Dashboard

  1. Go to Create > Import in Grafana.
  2. Use a prebuilt dashboard by entering an ID from the Grafana Dashboard Library (e.g., Docker Monitoring Dashboard ID: 12210).
  3. Select Prometheus as the data source and import.

Monitoring Docker Containers

1. Expose Docker Metrics

Enable Docker metrics on the host by modifying the Docker Daemon configuration:

  1. Add the following to /etc/docker/daemon.json:
   {
     "metrics-addr": "0.0.0.0:9323",
     "experimental": true
   }
Enter fullscreen mode Exit fullscreen mode
  1. Restart the Docker service:
   sudo systemctl restart docker
Enter fullscreen mode Exit fullscreen mode

2. Visualize Container Metrics

  • Prometheus will scrape metrics from the Docker Daemon.
  • Use Grafana dashboards to visualize metrics like:
    • Container CPU and memory usage
    • Network traffic
    • Disk I/O

Advanced Configuration

Alerting in Prometheus

  1. Add alert rules in prometheus.yml:
   rule_files:
     - "alert.rules"

   alerting:
     alertmanagers:
       - static_configs:
           - targets: ['host.docker.internal:9093']
Enter fullscreen mode Exit fullscreen mode
  1. Example alert.rules:
   groups:
     - name: example
       rules:
         - alert: HighCPUUsage
           expr: container_cpu_usage_seconds_total > 80
           for: 5m
           labels:
             severity: critical
           annotations:
             summary: "High CPU usage detected"
             description: "Container {{ $labels.container }} is using high CPU."
Enter fullscreen mode Exit fullscreen mode

Custom Dashboards in Grafana

  • Use query builders or PromQL to create custom panels and dashboards tailored to your monitoring needs.

Scale with Docker Swarm or Kubernetes

Deploy Prometheus and Grafana on orchestrators for better scalability and fault tolerance:

  • Use Kubernetes Helm charts for a simplified setup.
  • Integrate with monitoring tools like Kubernetes Metrics Server.

Best Practices

  1. Optimize Scrape Intervals:
    Adjust the scrape interval in Prometheus for high-traffic environments to avoid overloading the system.

  2. Use Persistent Storage:
    Mount volumes to persist data:

   volumes:
     - prometheus_data:/prometheus
     - grafana_data:/var/lib/grafana
Enter fullscreen mode Exit fullscreen mode
  1. Secure Access:

    • Use HTTPS for Grafana and Prometheus.
    • Set strong Grafana admin credentials.
  2. Automate Deployment:
    Use CI/CD pipelines to automate the deployment of monitoring stacks.


Conclusion

Using Docker to deploy Prometheus and Grafana offers a powerful, flexible, and scalable monitoring solution. With Docker's simplicity and containerization, setting up real-time monitoring becomes straightforward, ensuring your infrastructure and applications are running optimally.


Top comments (0)