Setting Up a Grafana Dashboard for Docker Logs with Loki and Promtail
Grafana Loki is a horizontally-scalable, highly-available, multi-tenant log aggregation system inspired by Prometheus, making it a great tool for analyzing Docker logs. In this guide, we’ll walk through setting up a Grafana dashboard for a Docker environment with Loki and Promtail.
Step 1: Docker Compose file
First, we need to create a docker-compose.yml
file to define our Grafana, Loki, and Promtail services. Save the following as docker-compose.yml
:
version: "3"
services:
grafana:
image: grafana/grafana:latest
ports:
- "3000:3000"
networks:
- loki
loki:
image: grafana/loki:latest
volumes:
- ./loki-data:/loki
networks:
- loki promtail:
image: grafana/promtail:latest
volumes:
- "/var/log:/var/log"
- "/var/lib/docker/containers:/var/lib/docker/containers"
- "/var/run/docker.sock:/var/run/docker.sock"
- "./promtail-config.yaml:/etc/promtail/config.yaml"
command: "-config.file=/etc/promtail/config.yaml"
networks:
- lokinetworks:
loki:
Step 2: Promtail Configuration
Next, we create the Promtail configuration. This file instructs Promtail which logs to scrape. Save the following as promtail-config.yaml
:
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /tmp/positions.yamlclients:
- url: http://loki:3100/loki/api/v1/pushscrape_configs:
- job_name: docker
pipeline_stages:
- docker: {}
static_configs:
- targets:
- localhost
labels:
job: docker
__path__: /var/lib/docker/containers/*/*-json.log
This configuration tells Promtail to scrape Docker logs from the /var/lib/docker/containers/*/*-json.log
path and send them to Loki.
Step 3: Run Docker Compose
With the docker-compose.yml
and promtail-config.yaml
files ready, we can run our Docker environment with the command:
docker-compose up
This command will start Grafana, Loki, and Promtail.
Step 4: Configure Grafana
Once you have all three components running, navigate to http://localhost:3000 in your web browser and you should see the Grafana login page. Use the default credentials admin
for both username and password, and you'll be prompted to change the password.
Next, we need to add Loki as a data source:
- Click the Gear Icon on the left panel and select “Data Sources”.
- Click “Add data source”.
- Select “Loki” from the list.
- For the URL, enter
http://loki:3100
. - Click “Save & Test”.
Loki is now set up as a data source!
Step 5: Visualize Docker Logs
Finally, it’s time to create a dashboard to display our Docker logs:
- Click the Plus Icon on the left panel and select “Dashboard”.
- Click “Add new panel”.
- From the Query dropdown, select “Loki”.
- In the Query field, enter your LogQL query,
{job="docker"}
. - Click “Apply” to save the panel.
- Click the Disk Icon at the top-right to save the dashboard.
You should now see a Grafana dashboard displaying your Docker logs!