How to run Grafana OSS in docker and docker-compose

Grafana is a multi-platform open source analytics and interactive visualization web application. It provides charts, graphs, and alerts for the web when connected to supported data sources.

Grafana connects with every possible data source, commonly referred to as databases such as Graphite, Prometheus, Influx DB, ElasticSearch, MySQL, PostgreSQL etc. Grafana being an open source solution also enables us to write plugins from scratch for integration with several different data sources. The tool helps us study, analyse & monitor data over a period of time, technically called time series analytics.

In this guide we will learn how to run Grafana using docker and docker-compose.

Related content:

Ensure that docker and docker compose is installed

Since we will be using docker to run our set up, it is important that it is installed and running. Please ensure that you are have docker installed. If you are using an Ubuntu system, checkout this guide on How to Install and Use Docker in Ubuntu 22.04.

Confirm that docker is working as expecting by checking the version:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$ docker version

Client:
 Cloud integration: v1.0.29
 Version:           20.10.21
 API version:       1.41
 Go version:        go1.18.7
 Git commit:        baeda1f
 Built:             Tue Oct 25 18:01:18 2022
 OS/Arch:           darwin/arm64
 Context:           default
 Experimental:      true

Server: Docker Desktop 4.14.1 (91661)
 Engine:
  Version:          20.10.21
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.7
  Git commit:       3056208
  Built:            Tue Oct 25 17:59:41 2022
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.6.9
  GitCommit:        1c90a442489720eec95342e1789ee8a5e1b9536f
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Next, ensure that docker-compose is installed. Docker compose is available as a python pip package. Ensure that python and pip is installed then install docker-compose with this command:

1
sudo pip3 install docker-compose

This is the version of docker-compose installed in my system:

1
2
$ docker-compose --version
Docker Compose version v2.12.2

Running Grafana in docker

To run Grafana with docker, use the following command. We are running the latest version of Grafana OSS – version 9.

1
2
3
docker run --name=grafana \
    -p 3000:3000 \
    grafana/grafana-oss:latest

You can use the -d argument to start in detached mode. Once the server is started, access it by visiting http://server_ip:3000 in your browser.

Install plugins in the Docker container

You can install official and community plugins listed on the Grafana plugins page or from a custom URL.

Install official and community Grafana plugins

Pass the plugins you want installed to Docker with the GF_INSTALL_PLUGINS environment variable as a comma-separated list. This sends each plugin name to grafana-cli plugins install ${plugin} and installs them when Grafana starts.

1
2
3
4
5
docker run -d \
  -p 3000:3000 \
  --name=grafana \
  -e "GF_INSTALL_PLUGINS=grafana-clock-panel,grafana-simple-json-datasource" \
  grafana/grafana-oss:latest

  Note: If you need to specify the version of a plugin, then you can add it to the GF_INSTALL_PLUGINS environment variable. Otherwise, the latest is used. For example: -e "GF_INSTALL_PLUGINS=grafana-clock-panel 1.0.1,grafana-simple-json-datasource 1.3.5"  

Save your Grafana data

If you do not designate a location for information storage, then all your Grafana data disappears as soon as you stop your container. To save your data, you need to set up persistent storage or bind mounts for your container.

1
2
3
4
5
6
7
# create a persistent volume for your data in /var/lib/grafana (database and plugins)
docker volume create grafana-storage

# start grafana
docker run -d -p 3000:3000 --name=grafana \
  -v grafana-storage:/var/lib/grafana \
  grafana/grafana-oss:latest

You may want to run Grafana in Docker but use folders on your host for the database or configuration. When doing so, it becomes important to start the container with a user that is able to access and write to the folder you map into the container.

1
2
3
4
5
6
7
8
mkdir data # creates a folder for your data
ID=$(id -u) # saves your user id in the ID variable

# starts grafana with your user id and using the data folder
docker run -d --user $ID \
  --volume "$PWD/data:/var/lib/grafana" \
  -p 3000:3000 \
  grafana/grafana-oss:latest

Using Docker-compose to run Grafana

We can add the instructions above to a docker compose file to make it easy for us to create, update and manage the deployments. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Add the following to a docker-compose.yaml file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
version: '3.9'

networks:
  grafana:

services:
  grafana:
    image: grafana/grafana-oss:latest
    ports:
      - 3000:3000
    volumes:
      - "$PWD/data:/var/lib/grafana"
    networks:
      - grafana

Save the file in the directory with the configurations then start the services with this command:

1
docker-compose up

Or you can add the -d argument for detached mode. Since we are binding the same ports, you can access in the same way as listed above.

Logging in to Grafana

Since we mounted the port to localhost:3000, head over to that url in your browser.

Login with your admin user (default admin/admin). You will be prompted to change your password the first time you login.

Conclusion

In this guide we managed to run Grafana using docker and docker-compose. You can now add data sources and create graphs.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy