Grafana on Docker using Terrafomr
• Mark Eschbach
Since I have converted to using Journalbeat and logging Docker to JournalD my life has become a bit easier. As part of that project I now have a repository of core tools which uses Terraform to manage the deployment of those containers. This includes an Influx instance.
Now it is time to bring the Grafana instance under the IaC control. First up is the basic declarations:
resource "docker_image" "grafana" {
name = "grafana/grafana:6.2.5"
}
resource "docker_container" "grafana" {
image = docker_image.grafana.name
name = "${local.slug}__grafana"
env = [ ]
networks_advanced {
name = docker_network.tools.id
}
log_driver = var.log_driver
ports {
internal = 3000
external = 3000
}
}
This will get the most basic configuration done. I use a variable for log_driver
since on my OSX laptop logging to
JournalD does not buy me anything. In a production setting this would be journald
.
Time to add some persistence so we can modify the container later and not lose our dashboards. According to the Grafana docs
the persistent data is stored at /var/lib/grafana
within the container. Something like the following should do it:
resource "docker_volume" "grafana" {
name = "${local.slug}__grafana"
}
resource "docker_container" "grafana" {
#...
volumes {
volume_name = docker_volume.grafana.name
container_path = "/var/lib/grafana"
}
}
This gives a simple Grafana instance which should survive restarts and such. Later I will need to figure out how to rebuild the dashboards.