avatarSzilárd Mátis

Summary

The provided web content outlines a detailed guide on setting up Grafana and Prometheus using Docker Compose for local development and monitoring.

Abstract

The article "Setup Grafana and Prometheus with Docker Compose" offers a comprehensive tutorial for running Grafana and Prometheus within Docker containers. It begins by introducing Grafana as an open-source platform for data visualization and analytics, emphasizing its compatibility with various data sources, including Prometheus. The guide includes instructions on creating a Docker Compose file for Grafana, specifying the necessary services, ports, environment variables, and volumes for seamless integration with Prometheus. It also provides an external configuration file example to connect Grafana with Prometheus automatically.

Prometheus, described as an open-source monitoring and alerting toolkit suitable for dynamic environments, is also set up using a Docker Compose file. The article explains how to configure Prometheus with its essential settings, such as scrape intervals and alert managers, and how to ensure data persistence through volumes.

The article further simplifies the process by suggesting the creation of a Makefile to manage the startup and shutdown of the containers. It concludes with validation steps to confirm the containers' operation, including using tools like lazydocker or the docker ps command, and provides the default credentials for accessing Grafana. Useful links to resources and documentation are included for further support.

Opinions

  • The author believes that using Docker Compose to set up Grafana and Prometheus simplifies the process and is beneficial for local development and monitoring.
  • The article suggests that Grafana's ability to connect with multiple data sources, including Prometheus, enhances its versatility and usefulness in various environments.
  • The inclusion of a Makefile for container management implies that the author values automation and ease of use in development workflows.
  • By providing a GitHub repository with the source code, the author indicates a commitment to open-source practices and community collaboration.
  • The recommendation of lazydocker as a tool for monitoring Docker containers reflects the author's preference for efficient and user-friendly interfaces.
  • The author's offer to support their work through a donation link suggests confidence in the article's utility and a willingness to engage with the reader community.

Setup Grafana and Prometheus with Docker Compose

How to run Grafana and Prometheus in Docker

This article will guide you trough to setup a Grafana and Prometheus instance in Docker and run them locally. The source code is available in the following GitHub repository: https://github.com/matisszilard/java-metrics.

Setup Grafana

Grafana is an open-source analytics and visualization platform primarily focused on time-series data. It allows the users to query, visualize, and understand their data through a variety of customizable dashboards and panels. Grafana supports a wide range of data sources, including popular databases like Prometheus, Graphite, Elasticsearch, MySQL, and many others.

It has a really good official documentation about how it can be run in Docker.

First, we need to create a docker compose file for Grafana with the following content:

services:
  grafana:
    image: grafana/grafana
    container_name: grafana
    ports:
      - 3000:3000
    restart: unless-stopped
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=grafana
    volumes:
      - ./grafana:/etc/grafana/provisioning/datasources
    networks:
      - java-metrics

networks:
  java-metrics:
    name: java-metrics
    driver: bridge

We provide the official image from Docker Hub, specify with a custom name under container_name. The ports variable defines the exposed ports from the docker image. We can inject environment variables, in this case we are setting the username and password as environment variables.

We also load an external config file in order to make the connection automatically to Prometheus:

apiVersion: 1

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

We also need to make a Docker network to make the connection between the two images:

networks:
  java-metrics:
    name: java-metrics
    driver: bridge

We can start the Grafana by running the following command:

$ docker-compose -f grafana/docker-compose.yml up -d

Setup Prometheus

Prometheus is an open-source monitoring and alerting toolkit. It was created to monitor the highly dynamic containerized environments common in modern cloud-native applications but can be used to monitor any type of system or service.

In case of Prometheus we also create a Docker compose file. We specify the config file and the docker volume where we can store the data. It is important to mount a volume otherwise we would loose all the saved data.

services:
  prometheus:
    image: prom/prometheus
    container_name: prometheus
    command:
      - "--config.file=/etc/prometheus/prometheus.yml"
    ports:
      - 9090:9090
    restart: unless-stopped
    volumes:
      - .:/etc/prometheus
      - prom_data:/prometheus
    networks:
      - java-metrics

volumes:
  prom_data:

networks:
  java-metrics:
    name: java-metrics
    driver: bridge

The Prometheus config files, it specifies some base configurations:

global:
  scrape_interval: 15s
  scrape_timeout: 10s
  evaluation_interval: 15s
alerting:
  alertmanagers:
    - static_configs:
        - targets: []
      scheme: http
      timeout: 10s
      api_version: v1
scrape_configs:
  - job_name: prometheus
    honor_timestamps: true
    scrape_interval: 15s
    scrape_timeout: 10s
    metrics_path: /metrics
    scheme: http
    static_configs:
      - targets:
          - prometheus:9090

We can start Prometheus similarly to Grafana:

docker-compose -f prometheus/docker-compose.yml up -d

Run the commands

In order to make our life easier we can create a Makefile to run the commands. The commands can be executed separately as well.

#   __  __       _         __ _ _
#  |  \/  |     | |       / _(_) |
#  | \  / | __ _| | _____| |_ _| | ___
#  | |\/| |/ _` | |/ / _ \  _| | |/ _ \
#  | |  | | (_| |   <  __/ | | | |  __/
#  |_|  |_|\__,_|_|\_\___|_| |_|_|\___|

.PHONY: all

all:
 echo hey

.PHONY: start
start: stop start-prometheus start-grafana

.PHONY: stop
stop: stop-prometheus stop-grafana

.PHONY: start-grafana
start-grafana: stop-grafana
 docker-compose -f grafana/docker-compose.yml up -d

.PHONY: stop-grafana
stop-grafana:
 docker-compose -f grafana/docker-compose.yml rm -s -f

.PHONY: start-prometheus
start-prometheus: stop-prometheus
 docker-compose -f prometheus/docker-compose.yml up -d

.PHONY: stop-prometheus
stop-prometheus:
 docker-compose -f prometheus/docker-compose.yml rm -s -f

Run make start to start the containers, and make stop to stop them:

We can validate if the containers are running with lazydocker or by running docker ps.

lazydocker output (https://github.com/jesseduffield/lazydocker)
docker ps output

Open http://localhost:3000/ and login in with the credentials from the Grafana Docker Compose file:

The Prometheus datasource should be already available in Grafana:

Useful links

https://hub.docker.com/r/grafana/grafana https://prometheus.io/ https://grafana.com/docs/grafana/latest/setup-grafana/ https://grafana.com/docs/grafana/latest/setup-grafana/installation/docker/ https://github.com/jesseduffield/lazydocker

If you think that my article was useful you can support my work here.

Programming
Grafana
Prometheus
Docker
Setup Guide
Recommended from ReadMedium