TIL: Docker Logging with Loki and Grafana on Synology
Tags: docker, loki, synology, todayilearned
Published: 2022-10-27 14:29:42

---
title: TIL: Docker Logging with Loki and Grafana on Synology
published: true
description: Docker Logging with Docker, Loki and Grafana
tags: #docker #loki #synology #til
cover_image: https://blog.brazdeikis.io/c/uploads/ian_taylor_j_Oq_Jbvo1_P9g_unsplash_5bca5012d0-1920.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2022-10-27 07:26 +0000
canonical_url: https://blog.brazdeikis.io/today-i-learned/docker-logging-loki-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](https://grafana.com/oss/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](https://raw.githubusercontent.com/grafana/loki/v2.6.1/production/docker-compose.yaml) from the official Loki GitHub repo:
```yaml
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
```

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:
```sh
docker plugin install grafana/loki-docker-driver:latest --alias loki --grant-all-permissions
```

### 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:
```json
{
	"log-driver": "loki",
	"log-opts": {
		"loki-url": "http://localhost:3100/loki/api/v1/push"
	}
}
```

### Restart Docker Daemon
It may vary depending on your system:
- Linux:
```sh
sudo systemctl start docker
```
- Synology:
```sh
sudo synopkgctl stop Docker
sudo synopkgctl start Docker
```

### Test Loki Docker configuration
All newly started containers will pick up new configuration. You can quickly check the current configuration of the running container:
```sh
> docker inspect -f '{{.HostConfig.LogConfig}}' $YOUR_CONTAINER_ID
{loki map[loki-url:http://localhost:3100/loki/api/v1/push]}
```
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](https://grafana.com/docs/loki/latest/operations/grafana/)

## Reference Links

- [Grafana Loki Docker Drive Client](https://grafana.com/docs/loki/latest/clients/docker-driver/)
- [Grafana docs](https://grafana.com/docs/loki/v2.6.x/operations/grafana/)