Easy Grafana and Docker-Compose Setup

Graham Bryan
The Startup
Published in
3 min readSep 4, 2020

My team has been working on solutions in order to quickly maintain, scale, and visualize internal software suites used for work in modeling and simulation, networking, and more. Before stumbling on Grafana, an incredible open source project for dashboard generation, we were coding our own custom plots in Javascript. The customization was nice, but it took a lot of time and it forced us to edit the source code often. From never knowing how to use Grafana it took no time to setup and we basically had all the same plots as before!

Now, we are able to launch an entire visualization suite alongside our Docker development environment. I wont get into the specifics of the environment, but our teams have generally switched to running all software development kits with docker and docker-compose. We are able to launch the SDK, a database, and the grafana visualization in one go. This allows for a seamless start-up for our developers and awesome looking plots. All you need is docker and docker-compose installed!

This quick article will show a grafana user to provision, version control, and add grafana to a docker-compose setup so a user can launch a powerful development and analysis tool in a single go.

Here is a look at the compose file and the associated Dockerfile.

Note, you could just launch the compose with image: grafana/grafana instead of using build: but this allows for more customization, easier user installation, and versioning.

If you wish anything to persist when you make changes to your dashboard or configurations, you must add a named volume to the setup. To create this volume,

$ docker volume create grafana-data

I am assuming a user already has or knows how to create a json model for the dashboard and add a data source. If you do not, you can still follow the setup and create your own dashboard later!

There are three things we add to the docker image: provisioning, grafana.ini, and dashboards. All of which are easily versioned controlled and can be released to update all your developers environments.

The provisioning is added so a user does not have to do any configuration to setup datasources or dashboards and are instead created automatically when launched. Here is what the structure looks like:

|-- provisioning
|-- dashboards
| `-- dashboard.yml
`-- datasources
`-- datasource.yml

The .yml are configurations for dashboards and datasources. For example they can look like,

The next configuration file, grafana.ini:

It is used to set multiple grafana configurations for ease of use. There are tons of possible options here that will meet most users requirements.

The dashboards directory contains your json model(s) for your dashboard(s).

|-- dashboards
| `-- my-dashboard.json

And that is it. This was a brief look at a quick way to integrate grafana into your workflow. With these created you can easily version control the entire setup and push it out to users. Launch the setup with,

$ docker-compose up

The docker image will be built for you and the service will launch the grafana homepage at http://localhost:9000.

As you continue and want to make this a more versatile setup you could add a database service and your own docker development environment. This would allow you to launch a powerful dev environment with a single command all connected via its own docker network.

[Edit: 12/2021]

Some users have had trouble with the code written here, so one has been nice enough to open source their setup in this library grafana-infrastructure using a mariadb backend. There are many ways to do this, but providing more examples to readers in one place is always helpful!

--

--

Graham Bryan
The Startup

I love applying software engineering and devops to modernize space applications!