Skip to content
Home » All Posts » How to Deploy Prometheus and Grafana with Docker Compose

How to Deploy Prometheus and Grafana with Docker Compose

Introduction

You may have heard Prometheus and Grafana mentioned together but have no idea exactly what they are. Don’t worry I was just like that. They are popular open-source tools often used together for monitoring and visualizing system and application performance. Prometheus is a monitoring and alerting toolkit that collects and stores time-series data, while Grafana is a visualization platform that allows users to create dashboards and alerts based on data from various sources (Prometheus being one of the many data sources it supports). In this blog, I will share with you how to quickly spin up Prometheus and Grafana for your monitoring needs.

To follow along with this article, you will need to install docker, docker compose and docker daemon service on your machine.

The Docker Compose File

Consider this simple docker-compose.yaml file that deploys Prometheus on port 9090 and Grafana on port 3000 on docker network pubnet which is a bridge network to your host machine.

version: '3.7'

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

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    volumes:
      - ./grafana-provisioning:/etc/grafana/provisioning
    networks:
      - pubnet
    ports:
      - "3000:3000"
    depends_on:
      - prometheus

networks:
  pubnet:
    name: pubnet
    driver: bridge

Prometheus Configuration File

The ./prometheus.yml file referenced in the docker compose file configures the behavior of Prometheus. For example, the file below configures the global scrape intervals and defines 2 scrape jobs called prometheus and myjob, both having one or more endpoints to fetch data from.

global:
  scrape_interval: 15s
  scrape_timeout: 10s
  evaluation_interval: 15s

alerting:
  alertmanagers:
    - static_configs:
        - targets: []

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets:
          - localhost:9090 # replace with real data source
        labels:
          app: prometheus

  - job_name: 'myjob'
    static_configs:
      - targets:
        - datasource1:9404 # replace with real data source
        - datasource2:9405 # replace with real data source
        - datasource3:9406 # replace with real data source

Grafana Provisioning Directory

The volume reflection./grafana-provisioning:/etc/grafana/provisioning mentioned in docker compose file maps ./grafana-provisioning folder from the host machine to the Grafana container when deployed. This folder should contain all the necessary configuration files, data sources or dashboard templates that Grafana should provision upon deployment. Having this folder prepared could save a lot of setup efforts. This is a simple grafana-provisioning folder with datasources and dashboards sub folders,

grafana-provisioning/
        |----> dashboards/
               |----> dashboards.yaml
               |----> jvm-dashboard.json
               |----> mydashboard.json
        |----> datasources/
               |----> datasource.yaml

dashboards.yaml tells Grafana where to find and preload dashboard files (/etc/grafana/provisioning/dashboards).

apiVersion: 1
providers:
  - name: 'myprovider'
    orgId: 1
    folder: 'myfolder'
    type: file
    disableDeletion: false
    allowUiUpdates: true
    updateIntervalSeconds: 30
    options:
      # IMPORTANT: point to the folder where your JSONs live
      path: /etc/grafana/provisioning/dashboards
      foldersFromFilesStructure: true

datasource.yaml that tells Grafana to create a default data source that fetches data from prometheus at http://prometheus:9090. The host name prometheus correspond to the docker container name and your docker daemon will automatically resolve to the correct IP address.

apiVersion: 1
datasources:
  - name: Prometheus
    type: prometheus
    access: proxy
    orgId: 1
    url: http://prometheus:9090
    isDefault: true
    editable: true

Grafana Dashboards

If you were to deploy Prometheus and Grafana for the first time, you may not have dashboard JSON files to load. You could remove the dashboards folder along with the dashboards.yaml from grafana-provisioning folder entirely. You could create your own dashboards on Grafana GUI and export them as JSON files to be include in grafana-provisioning next time you deploy.

Start the Services

Once you have prepared the above folders and files, you could start the services by:

docker compose -f docker-compose.yaml up -d

Confirm that the services are indeed running:

docker compose -f src/monitoring/docker-compose.yaml ps

Stop the services:

docker compose -f docker-compose.yaml stop
docker compose -f docker-compose.yaml down ---> stop and remove containers

Summary

With a small Compose stack, you now have a durable Prometheus + Grafana setup.If you do not have a data source in hand and would like to set up one to observe the power of Grafana, I will share that in the next blog.

Join the conversation

Your email address will not be published. Required fields are marked *