Vivek Viswanathan
Vivek Viswanathan Staff Engineer at Compass. Passionate about building software that pushes hardware to its limits!!

Start Grafana on Docker

Start Grafana on Docker

Grafana is a popular opensource tool for visualizing metrics from various data sources. We have the popular alpine docker build available from dockerhub itself. Like any other tool that requires persistent volume (if you want the data to be saved across restarts), we need to have a local volume created. This blog is all about starting Grafana with persistent volume mount configured against a postgres datasource.

Pull the required docker file

Identify the required docker image to be pulled using the link: https://hub.docker.com/r/grafana/grafana/tags

Now use the docker pull command to get the image:

$ docker pull grafana/grafana:<tag>

Note: If you receive the below output on issuing the above command, then it means the current runtime user isn't part of the docker group.

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: 
Post http://%2Fvar%2Frun%2Fdocker.sock/v1.39/images/create?fromImage=postgres&tag=12: 
dial unix /var/run/docker.sock: connect: permission denied

Add the runtime user to the docker group (refer: https://docs.docker.com/install/linux/linux-postinstall) or run the command as root (i.e.) sudo docker pullgrafana/grafana:<tag>

Create a local persistent data directory

Since we don't want to lose the data when the service stops, we have to mount a location for data storage before we start the grafana service:

mkdir -p $HOME/docker/volumes/grafana/data

Run grafana service

Run the below command now to start the service on docker:

$ docker run -d \
--name=grafana \
-p 3000:3000 \
-v grafana-storage:$HOME/docker/volumes/grafana/data \
grafana/grafana:<tag>

Now try accessing http://localhost:3000 & it should load the grafana login page. The default admin & password is admin/admin. Change this as the first step to avoid any security vulnerability.

Connect to postgres datasource running locally

If you are running your postgres locally, then docker provides a good solution with "host.docker.internal" in macOS. This refers to the host on which docker is running and can be used to configure any datasource.

Click on the "Add Datasource" and select "postgres" with host as "host.docker.internal" if the postgres is running locally. Rest is self explanatory on the form. Post the right configuration, "Save & Test" should spill out the message: "Database Connection OK"

Run grafana service with plugins

Typically we would need additional plugins so that we could visualize data better. Fortunately, it is possible to pass a list of plugins as env variable so that grafana cli can install the plugins before starting.

$ docker run -d \
--name=grafana \
-p 3000:3000 \
-v grafana-storage:$HOME/docker/volumes/grafana/data \
-e "GF_INSTALL_PLUGINS=grafana-clock-panel,grafana-piechart-panel,grafana-simple-json-datasource"
grafana/grafana:<tag>

comments powered by Disqus