Depending on the number of environments in which you have monitoring enabled, it may be cumbersome to keep all your Grafana dashboards synchronized across environments when changes occur.
In this article, I would like to show a Docker image I created to keep Grafana dashboards in sync across environments.
What's in the Image
How to Use It
With all options:
docker run --name automated-grafana -d -p 9090:9090 -p 6666:6666 \ -e "ENVIRONMENT=prod" \ -e "GF_SERVER_HTTP_PORT=6666" \ -e "WAITING_TIME=20" \ -v `pwd`/prometheus-config:/prometheus-config \ -v `pwd`/dashboards:/dashboards \ -v `pwd`/users:/users \ -v `pwd`/sources:/sources \ serragnoli/automated-grafana-prometheus
-p 9090:9090: Exposes the Prometheus default port to the host.
-p 6666:6666: Exposes a custom Grafana port that was determined by
-e "GF_SERVER_HTTP_PORT=6666"; both of these parameter values need to match.
-e "ENVIRONMENT=prod": Determines which Prometheus config to use. Ensure that a
prometheus-<env> .ymlsuffixed with the environment where you'll deploy to exists in the directory. In this example, the value is
/prometheus-config/prometheus-prod.ymlis expected to exist.
-e "WAITING_TIME=20": By default, each JSON file is loaded 10 seconds apart from each other. There may be situations that 10 seconds may be too much or too little time. When that's the case, use this variable to control the interval of loading each JSON file.
-v `pwd`/sources:/sources: Map the directories containing the JSON files to be loaded by Grafana in the container. It's worth mentioning that the
/dashboardsdirectory must only contain dashboard JSON files and the same applies to the other directories.
After running the above command, you should have Prometheus running on
localhost:9090 and Grafana running on
You can run the same image everywhere; just use a Prometheus config file that points to
/metricsendpoints for the environment you're deploying to.
You can load as many dashboards, users, and data-source JSON files as you want by only mapping the directories that contain these files from the host to the container.
You get fewer moving parts in your monitoring system. Therefore, there are fewer things to worry about, i.e. lost/slow network connectivity between Prometheus and Grafana.
Difficulty to scale each component individually as they are in the same container;
Breaking the Docker rule of thumb one process per Docker container.
I've added sample JSON files to the image page in DockerHub:
I can't tell if I have tried to reinvent the wheel with this image, but the is fact that I couldn't find anything already automating Grafana dashboards in the same way.
As I mentioned above, this is currently my favorite solution and if I learn a better way of doing it, I'll update this article.