Tools

How to set up Grafana with Docker and connect it to Prometheus

Including Grafana in a monitoring stack allows us to effectively keep an eye on an application status. Project monitoring becomes much easier once we add an advanced analysing platform to display collected data in a rich set of charts. We can easily set up Grafana on our development environment using Docker Compose to test the tool.

What we’re going to build

In this article, I’m going to work with the spring-boot-log4j-2-scaffolding project where I already use Prometheus to collect data on a Spring Boot application. You can learn on how to monitor project like this in the following posts:

My end goal was to see metrics gathered by Prometheus in the Grafana JVM dashboard.

Enhance application monitoring with Grafana

Let’s see how easy it is to set up this tool on a local environment, connect it to a Prometheus server and add a dashboard with a comprehensive set of graphs.

How to set up a Grafana service with Docker Compose

In order to test Grafana on a local machine we’re going to use the official image and the following Docker Compose configuration:

# docker-compose.yml
version: "3.3"
services:
…
  grafana:
    image: grafana/grafana:$GRAFANA_VERSION
    environment:
      GF_SECURITY_ADMIN_USER: $GRAFANA_ADMIN_USER
      GF_SECURITY_ADMIN_PASSWORD: $GRAFANA_ADMIN_PASSWORD
    ports:
      - 3000:3000
    volumes:
      - grafana-storage:/var/lib/grafana
    depends_on:
      - prometheus
    networks:
      - internal
…
networks:
  internal:

volumes:
  grafana-storage:
…

The prometheus service is already defined in the same docker-compose.yml file. Furthermore, it’s is available for the other services running within the internal network under prometheus:9090 (http://localhost:9090/ in my browser).

Consequently, we have to define default values for the environment variables. I’m going to specify them in the following .env file:

# .env
GRAFANA_VERSION=7.1.3
GRAFANA_ADMIN_USER=user
GRAFANA_ADMIN_PASSWORD=test

It’s possible to configure data sources and dashboards before starting Grafana for the first time. You can learn how to do it in the Grafana provisioning – How to configure data sources and dashboards post. On the other hand, you can achieve the same results with the UI. Below you’ll find a step-by-step instruction on how to set up a data source and a dahsboard manually.

Start the grafana service

Run the service with the $docker-compose up -d grafana command and visit http://localhost:3000/ in your browser. You’ll see the login form, where you should enter the credentials specified in the container configuration. In my example I have to use my default data (from my .env file):

  • username: user,
  • password: test.

However, if you start the service without setting the environment variables for admin credentials, Grafana defaults to:

  • username: admin,
  • password: admin.

In this case, you’ll see a prompt to change the password as soon as you log in.

Configuring data sources manually

There are no monitoring data yet and the Dashboard section visible on the screenshot below has no entries.

First, visit http://localhost:3000/datasources and click the Add data source button:

Choose one data source from the list of the supported data sources. In my case, I click Prometheus:

In the following step specify a unique name for this source, its url and the access mode:

Click the Save & Test button to verify the configuration:

Apply a ready to use dashboard

Grafana offers a rich set of predefined dashboards. For monitoring my Spring Boot project I’m going to use the JVM dashboard. Visit http://localhost:3000/dashboard/import and either upload the configuration saved as a JSON file or paste the dashboard ulr (https://grafana.com/grafana/dashboards/4701):

Next, confirm the settings by clicking the Import button:

Finally, you should be able to see the monitoring data presented in the dashboard:

More on how to set up Grafana

little_pinecone

Recent Posts

Simplify the management of user roles in Spring Boot

Spring Security allows us to use role-based control to restrict access to API resources. However,…

3 years ago

Create a custom annotation to configure Spring Boot tests

A custom annotation in Spring Boot tests is an easy and flexible way to provide…

3 years ago

Keycloak with Spring Boot #4 – Simple guide for roles and authorities

Delegating user management to Keycloak allows us to better focus on meeting the business needs…

3 years ago

Keycloak with Spring Boot #3 – How to authorize requests in Swagger UI

Swagger offers various methods to authorize requests to our Keycloak secured API. I'll show you…

3 years ago

Keycloak with Spring Boot #2 – Spring Security instead of Keycloak in tests

Configuring our Spring Boot API to use Keycloak as an authentication and authorization server can…

3 years ago

Keycloak with Spring Boot #1 – Configure Spring Security with Keycloak

Keycloak provides simple integration with Spring applications. As a result, we can easily configure our…

3 years ago