Grafana is a well-known open-source visualization tool that allows admins to create and view interactive dashboards for monitoring environments. Docker is a tool that simplifies the deployment of applications, including Grafana, and allows managing them much easier. Let’s consider how to run and configure Docker Grafana image deployments and the steps involved.
What is Docker?
Docker is the de facto standard for running containers in the enterprise. It makes deploying applications much easier, including application:
- Building
- Packaging
- Deployment
A container is a standalone executable package that contains everything needed to run an application, including code, runtime requirements, tools, libraries, and configuration settings.
Containers allow developers to have a consistent application environment, regardless of the container host the application image is running upon. It makes developing, testing, and deploying applications across different platforms and environments easy.
Many Docker images are available for standard applications, making it extremely easy to spin up new applications in the enterprise. Most images are readily available on Docker Hub for download.
What is Grafana?
Grafana is an open-source, feature-rich data visualization and monitoring tool that allows you to create, explore, and share dashboards. Grafana supports data sources like Prometheus, InfluxDB, Elasticsearch, and more. It provides various visualization options, including graphs, tables, heatmaps, and gauges, to help you analyze and understand your data.
What is Telegraf?
Telegraf is an open-source, plugin-driven server agent for collecting and reporting metrics. It is part of InfluxData’s TICK stack, comprising Telegraf, InfluxDB, Chronograf, and Kapacitor. Telegraf can collect metrics from various sources, such as system metrics and third-party APIs, or even listen for metrics sent by other applications and then send the collected data to a wide array of data stores, such as InfluxDB, Prometheus or Grafana’s native data storage.
You can learn more about Telegraf and download it here: Telegraf | InfluxData
In conjunction with Grafana, Telegraf plays a crucial role in collecting and forwarding metrics data to be visualized and analyzed in Grafana dashboards. Using Telegraf as a metrics collector, you can use its extensive library of input, output, and processor plugins to gather data from various sources and send it to Grafana or other compatible data stores.
Prepare Telegraf for InfluxDB and Docker
To set up Telegraf for InfluxDB and Docker:
- Install Telegraf on your host system or run it as a Docker container.
- Create a Telegraf configuration file with the input plugins for collecting Docker metrics and the output plugins for sending the data to InfluxDB.
- Start the Telegraf agent with the configuration file.
Setting up Prometheus to Run on Docker
Prometheus is an open-source monitoring and alerting toolkit designed for reliability and scalability. To set up Prometheus to run on Docker:
- Create a Prometheus configuration file specifying the targets and the scrape interval.
- Pull the official Prometheus Docker image using the following command:
docker pull prom/prometheus
- Run a Prometheus container with the configuration file using the following command:
docker run -d -p 9090:9090 --name=prometheus -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
This command will start a new Prometheus container, expose it on port 9090, and use a persistent volume mount for the configuration file.
Creating a configuration file for Telegraf and Docker
To create a configuration file for Telegraf and Docker, follow these steps:
- Create a new file named telegraf.conf.
- Configure the input plugins for collecting Docker container metrics by adding the following lines to the file:
[[inputs.docker]] endpoint = "unix:///var/run/docker.sock" gather_services = false
- Configure the output plugins for sending the data to InfluxDB by adding the following lines to the file:
[[outputs.influxdb]] urls = ["http://influxdb:8086"] database = "telegraf"
- Save the file and start the Telegraf agent with the configuration file using the following command:
telegraf --config telegraf.conf
Installing Grafana on Docker
To install Grafana on Docker:
- Pull the official Grafana Docker image using the following command:
docker pull grafana/grafana
- Run a Grafana container with the following command:
docker run -d -p 3000:3000 --name=grafana grafana/grafana
This command will start and expose a new Grafana container on port 3000.
You can use additional parameters to configure persistent storage. - Set up persistent storage for Grafana data using Docker volumes or bind mounts. For example:
docker run -d -p 3000:3000 --name=grafana -v grafana-storage:/var/lib/grafana grafana/grafana
This command creates a named volume called grafana-storage and mounts it to /var/lib/grafana inside the container.
- Configure environment variables for Grafana, such as the admin password, by adding the -e flag to the docker run command. For example:
docker run -d -p 3000:3000 --name=grafana -e "GF_SECURITY_ADMIN_PASSWORD=mysecret" grafana/grafana
- Enable Grafana plugins by adding the GF_INSTALL_PLUGINS environment variable and specifying a comma-separated list of plugin names. For example:
docker run -d -p 3000:3000 --name=grafana -e "GF_INSTALL_PLUGINS=grafana-clock-panel
Running and configuring the Grafana docker container efficiently monitors and visualizes metrics from various sources.
Accessing the Docker Metrics
To access the Docker metrics collected by Telegraf and stored in InfluxDB:
- Access the Grafana UI at http://localhost:3000.
- Log in with the default admin user and password (admin/admin).
- Create a new data source for InfluxDB with the Telegraf database.
- Create a new dashboard and add panels to visualize the Docker metrics.
Running Prometheus and Grafana using Docker Compose
To run Prometheus and Grafana on Docker simultaneously, you can use Docker Compose. Create a docker-compose.yml file with the following content:
version: "3" services: prometheus: image: prom/prometheus volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml ports: - "9090:9090" grafana: image: grafana/grafana volumes: - grafana-storage:/var/lib/grafana ports: - "3000:3000" volumes: grafana-storage:
Then, run docker-compose up -d to start both services in daemon mode.
Creating Visualization in Grafana
To create visualizations in Grafana:
- Access the Grafana UI at http://localhost:3000.
- Log in with the admin user and password.
- Create a new dashboard by clicking on the “+” icon in the left sidebar and selecting “Dashboard.”
- Add a new panel to the dashboard by clicking on the “Add Panel” button.
- Select the data source (Prometheus or InfluxDB) and configure the panel’s query and visualization options.
- Save the dashboard, share it with your team, or embed it in other applications.
Custom Grafana Docker Image
Sometimes, you may want to create a custom Grafana Docker image with pre-installed plugins, custom configuration files, or other customizations. To do this, you can create a Dockerfile and build a new image based on the official Grafana Docker image.
- Create a Dockerfile with the following content:
FROM grafana/grafana # Install plugins ENV GF_INSTALL_PLUGINS=grafana-clock-panel,grafana-simple-json-datasource # Copy custom configuration files COPY grafana.ini /etc/grafana/grafana.ini COPY custom.ini /etc/grafana/custom.ini
- Build the custom Grafana Docker image using the following command:
docker build -t my-grafana
- Run a container based on the custom image:
docker run -d -p 3000:3000 --name=grafana my-grafana
Managing Grafana Data
Grafana stores its data in the /var/lib/grafana directory inside the container. You can use Docker volumes or bind mounts to persist this data across container restarts and upgrades.
- Using Docker volumes:
docker run -d -p 3000:3000 --name=grafana -v grafana-storage:/var/lib/grafana grafana/grafana
This command creates a named volume called grafana-storage and mounts it to /var/lib/grafana inside the container.
- Using bind mounts:
docker run -d -p 3000:3000 --name=grafana -v /path/to/grafana/data:/var/lib/grafana grafana/grafana
This command mounts a local directory (/path/to/grafana/data) to /var/lib/grafana inside the container.
Ensure that the local directory and its contents have the correct permissions for the Grafana user (UID 472) to read and write data.
Grafana CLI Plugins
Grafana CLI is a command-line tool for managing Grafana installations. It can be used to install and update plugins, reset the admin password, and perform other tasks.
To run Grafana CLI commands inside a running Grafana container, use the docker exec command. For example, to list installed plugins:
docker exec -it grafana grafana-cli plugins ls
InfluxDB Database Configuration
InfluxDB is a popular time-series database that can be used as a data source for Grafana. To store Docker metrics in InfluxDB, you must configure Telegraf to collect them and write them to an InfluxDB database.
- Install InfluxDB on your host or run it as a Docker container:
docker run -d -p 8086:8086 --name=influxdb influxdb
- Create a new InfluxDB database for Docker metrics:
curl -X POST 'http://localhost:8086/query' --data-urlencode 'q=CREATE DATABASE telegraf'
- Update the Telegraf configuration file (telegraf.conf) to enable the InfluxDB output plugin and configure it to use the new database:
[[outputs.influxdb]] urls = ["http://localhost:8086"] database = "telegraf"
- Restart the Telegraf container to apply the new configuration:
docker restart telegraf
Adding Redis Data Source for Grafana Plugin
If you want to monitor a Redis instance using Grafana you can install a Grafana plugin for Redis and configure it as a data source. The following steps demonstrate how to add a Redis data source to Grafana:
- Install the Redis plugin using Grafana CLI inside the Grafana container:
docker exec -it grafana grafana-cli plugins install redis-datasource
- Restart the Grafana container to load the new plugin:
docker restart grafana
- Log in to Grafana UI and navigate to “Configuration” > “Data Sources.”
- Click “Add data source” and select “Redis” from the list of available data sources.
- Provide the necessary information to connect to your Redis instance, such as the URL, port, and password (if required). Save the configuration.
Now you can use the Redis data source to create panels and dashboards in Grafana that display Redis metrics.
Monitoring Docker Containers with a Grafana Dashboard
Once Grafana is set up and the necessary data sources are added, you can create custom dashboards to monitor your Docker containers. Combining InfluxDB, Prometheus, and Redis data can create a comprehensive view of your containers’ performance, resource usage, and overall health.
Creating a Docker Container Dashboard
- Log in to the Grafana UI and, click the “+” icon on the left sidebar, then select “Dashboard.”
- Click “Add new panel” to start creating a new visualization.
- Choose the data source for your panel, such as InfluxDB for Docker container metrics or Prometheus for application-specific metrics.
- Write a query to fetch the data you want to visualize. For example, to display the CPU usage of a specific Docker container, you can use a query like:
SELECT mean("usage_percent") FROM "docker_container_cpu" WHERE "container_name" = 'your-container-name' AND $timeFilter GROUP BY time($__interval) fill(null)
- Choose the appropriate visualization type, such as a graph or a gauge, and customize the panel settings as needed.
- Save the panel and the dashboard.
Below is a look at how you can use the metrics pulled from the data sources to build custom queries.
Expanding the metrics from the data sources.
Importing Pre-built Grafana Dashboards
Grafana offers a variety of pre-built dashboards created by the community that can help you monitor your Docker containers quickly. To import a pre-built dashboard:
- Visit the Grafana Dashboards page and search for a dashboard related to Docker or your specific data source.
- Click on the desired dashboard and note the dashboard ID.
- Log in to the Grafana UI, click the “+” icon on the left sidebar, and select “Import.”
- Enter the dashboard ID and click “Load.”
- Select the data sources you want to use with the dashboard and click “Import.”
You can now use the imported dashboard to monitor your Docker containers and customize it as needed.
Setting Up Alerts in Grafana
Grafana allows you to set up alerts based on specific conditions in your data, such as high CPU usage or low available memory. To set up an alert:
- Open a dashboard with a panel you want to create an alert for.
- Click the panel title and select “Edit.”
- Click the “Alert” tab and click “Create Alert.”
- Configure the alert conditions, evaluation interval, and notification settings.
- Save the panel and the dashboard.
Grafana will now monitor your data and trigger the specified alert action, such as sending an email or a webhook, when the alert conditions are met.
Wrapping up
By combining Docker, InfluxDB, Telegraf, and Grafana, among others, you can have a robust monitoring solution that runs on cloud-native technologies. Docker containers make provisioning these solutions extremely easy since the application image contains everything needed for the application to run as expected, including prerequisites. Grafana contains many open-source, readily available dashboards you can easily download and import into the environment, so you don’t have to build dashboards for monitoring manually.