Monitoring and observability are crucial components of any modern application stack. Grafana and Prometheus are two of the most popular tools used in tandem to achieve this. Prometheus is a powerful monitoring and alerting system, while Grafana provides a flexible and beautiful way to visualize the data collected by Prometheus.
In this blog post, we will walk through the steps to deploy Grafana and Prometheus using Docker Compose, enabling you to quickly set up a monitoring stack for your applications.
Prerequisites
Before we start, make sure you have the following installed:
- Docker: Docker should be installed on your machine. You can follow the installation guide for your operating system here.
- Docker Compose: Docker Compose is a tool for defining and running multi-container Docker applications. Install it by following the guide here.
Step 1: Set Up a Project Directory
First, let's create a directory to store our Docker Compose configuration and related files. Open your terminal and run:mkdir grafana-prometheus cd grafana-prometheus
Step 2: Create the Docker Compose File
In the project directory, create a docker-compose.yml
file. This file will define the services for Prometheus and Grafana.touch docker-compose.yml
Open the file in your favorite text editor and add the following content:version: '3.7' services: prometheus: image: prom/prometheus:latest container_name: prometheus volumes: - ./prometheus:/etc/prometheus/ command: - '--config.file=/etc/prometheus/prometheus.yml' ports: - "9090:9090" grafana: image: grafana/grafana:latest container_name: grafana ports: - "3000:3000" volumes: - grafana-storage:/var/lib/grafana depends_on: - prometheus volumes: grafana-storage:
This docker-compose.yml
file defines two services:
- Prometheus:
- Uses the
prom/prometheus
Docker image. - Exposes port
9090
, which is the default port for Prometheus. - Mounts a volume for the Prometheus configuration file.
- Grafana:
- Uses the
grafana/grafana
Docker image. - Exposes port
3000
, which is the default port for Grafana. - Creates a named volume
grafana-storage
to persist Grafana data.
Step 3: Configure Prometheus
Now, let's configure Prometheus. Create a directory named prometheus
inside your project directory:mkdir prometheus
Inside the prometheus
directory, create a prometheus.yml
file:touch prometheus/prometheus.yml
Add the following content to prometheus.yml
:global: scrape_interval: 15s scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090']
This configuration tells Prometheus to scrape metrics from itself every 15 seconds.
Step 4: Start the Services
With everything configured, you can now start the Prometheus and Grafana services using Docker Compose. Run the following command in your terminal:docker-compose up -d
This command will download the necessary Docker images and start the containers in the background.
Step 5: Access the Prometheus and Grafana Dashboards
Once the services are up and running, you can access the dashboards in your web browser:
- Prometheus: Go to
http://localhost:9090
- Grafana: Go to
http://localhost:3000
Step 6: Add Prometheus as a Data Source in Grafana
To visualize Prometheus metrics in Grafana, you need to add Prometheus as a data source:
- Log in to Grafana using the default credentials (
admin
/admin
). You will be prompted to change the password after the first login. - Click on the gear icon (⚙️) on the left sidebar to access the Configuration menu.
- Click on "Data Sources" and then "Add data source."
- Select "Prometheus" from the list of available data sources.
- In the "URL" field, enter
http://prometheus:9090
(this works because Docker Compose sets up networking between the containers). - Click "Save & Test" to verify the connection.
Step 7: Create a Dashboard in Grafana
Now that Prometheus is set up as a data source in Grafana, you can create your first dashboard:
- Click on the "+" icon on the left sidebar and select "Dashboard."
- Click on "Add New Panel."
- In the "Metrics" tab, select "Prometheus" as the data source.
- Enter a Prometheus query in the "Query" field (e.g.,
up
to see the status of monitored targets). - Customize the visualization and click "Apply" to save the panel.
You can add multiple panels to a single dashboard, each representing different metrics from Prometheus.
Step 8: Persisting Data (Optional)
By default, the Grafana and Prometheus containers do not persist data across restarts. To make the data persistent, ensure that the volumes are correctly configured in your docker-compose.yml
file. In the example provided, Grafana’s data is already persisted using the grafana-storage
volume.
For Prometheus, you can add a volume to store the time-series data:prometheus: image: prom/prometheus:latest container_name: prometheus volumes: - ./prometheus:/etc/prometheus/ - prometheus-data:/prometheus command: - '--config.file=/etc/prometheus/prometheus.yml' ports: - "9090:9090" volumes: prometheus-data:
Conclusion
Deploying Grafana and Prometheus using Docker Compose is a straightforward way to set up a powerful monitoring and visualization stack. With just a few configuration files, you can have a fully functional environment that helps you monitor and analyze the performance of your applications.
This setup is perfect for development and testing environments. For production use, you might want to explore more advanced configurations, such as scaling, security, and high availability.
If you found this guide helpful, or if you have any questions or suggestions, feel free to leave a comment below. Happy monitoring!