DEV Community

Cover image for TIL: Docker Logging with Loki and Grafana on Synology
Sergej Brazdeikis
Sergej Brazdeikis

Posted on • Originally published at blog.brazdeikis.io

TIL: Docker Logging with Loki and Grafana on Synology

Recently I wanted to set up and explore a logging tool for Docker, so all the logs would go into one location. After a quick research, I decided to try out Loki. It natively integrates with Grafana and has Prometheus vibes :)

Loki and Docker setup

Let's install the Loki and Grafana combination via Docker compose I used this template from the official Loki GitHub repo:

version: "3"

networks:
  loki:

services:
  loki:
    image: grafana/loki:2.6.0
    ports:
      - "3100:3100"
    command: -config.file=/etc/loki/local-config.yaml
    networks:
      - loki

  promtail:
    image: grafana/promtail:2.6.0
    volumes:
      - /var/log:/var/log
    command: -config.file=/etc/promtail/config.yml
    networks:
      - loki

  grafana:
    image: grafana/grafana:latest
    ports:
      - "3000:3000"
    networks:
      - loki
Enter fullscreen mode Exit fullscreen mode

It also comes with promtail, which pipes the system logs into Loki.

For the first login into Grafana - use admin for username and password.

Configure Docker to route logs to Loki

Install Docker Loki plugin

Grafana Loki supports Docker plugin and integrates natively with it.

There is Docker plugin which simplifies log routing. To install it:

docker plugin install grafana/loki-docker-driver:latest --alias loki --grant-all-permissions
Enter fullscreen mode Exit fullscreen mode

Configure Docker to route logs to Loki

For this, we need to locate dockerd.json. It may be in different locations:

  • Linux default location: /etc/docker/daemon.json
  • Synology: /var/packages/Docker/etc/dockerd.json

Create or alter dockerd.json to include the following:

{
    "log-driver": "loki",
    "log-opts": {
        "loki-url": "http://localhost:3100/loki/api/v1/push"
    }
}
Enter fullscreen mode Exit fullscreen mode

Restart Docker Daemon

It may vary depending on your system:

  • Linux:
sudo systemctl start docker
Enter fullscreen mode Exit fullscreen mode
  • Synology:
sudo synopkgctl stop Docker
sudo synopkgctl start Docker
Enter fullscreen mode Exit fullscreen mode

Test Loki Docker configuration

All newly started containers will pick up new configuration. You can quickly check the current configuration of the running container:

> docker inspect -f '{{.HostConfig.LogConfig}}' $YOUR_CONTAINER_ID
{loki map[loki-url:http://localhost:3100/loki/api/v1/push]}
Enter fullscreen mode Exit fullscreen mode

P.S. docker ps to get the list of running containers. Just in case ;)

Confuguring Grafana to query logs

For this part of the docs, I think it is best to use the official page since it is clearly described here - Configure Loki as a source in Grafana

Reference Links

Top comments (0)