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

featured image

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:

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:

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.
Login page where we use the default credentials set in the docker-compose configuration

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.

Grafana welcome screen

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

We have to add the data source to be able to monitor an app with Grafana

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

Choose Prometheus as the data source

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

Set up a Prometheus serves as the Grafana data source

Click the Save & Test button to verify the configuration:

Successful data source 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):

To use a predefined dashboard we have to import it

Next, confirm the settings by clicking the Import button:

Importing the JVM dashboard

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

More on how to set up Grafana

Leave a Reply

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