Use Loki with Docker without any configuration! (mostly)
A service discovery mechanism for Grafana Loki (actually Promtail) that discovers targets (log files) directly from a running Docker daemon
Docker capabilities of upstream Loki may be insufficient, as they require
changing the Docker daemon configuration (/etc/docker/daemon.json) or setting
--log-opts on every container, which may not be desired or even impossible
(e.g. in provided / managed environments):
- Loki Docker logging
driver
requires installing a Docker plugin on every machine the Docker daemon runs
on, also
--log-driver=lokimust be passed to everydocker run. Very tedious - Others
suggest using
--log-opt tag=...to "hook in" container information and pass that out using relabel rules. Again, requires setting something on every container (or in/daemon.json), which is tedious.
The Kubernetes capability of Loki however do not face this problem, as
kubernetes_sd hands Promtail the container ID, which can be used to grab the
correct logs from /var/log/docker/containers, while also retaining lots of
__meta_ labels for identifying logs.
But Loki knows no docker_sd so we are out of luck? No!
Docker knows file_sd and the we can docker ls containers to get their ID.
And it gets even better, docker inspect even tells us LogPath – the absolute
path to the JSON file the containers logs are in.
loki-docker-sd leverages that to write targets to a JSON file suitable for
file_sd and sets __path__ to LogPath so that Promtail can easily tail
our containers logs.
There is a docker container at shorez/loki-docker-sd. Requires access to /var/run/docker.sock:
$ docker run -v /var/run/docker.sock:/var/run/docker.sock:ro shorez/loki-docker-sdFor a complete example, see docker-compose.yml
You said without configuration. LIAR
Yeah. lol. No Docker configuration at least and that's what counts.
A bit of Promtail configuration is still required (but that's not an issue). See
example-config.yml for that
loki-docker-sd provides the following labels during relabeling:
__meta_docker_container_id: Docker container ID__meta_docker_container_name: Docker container name__meta_docker_container_status: Docker container status (running, etc.)__meta_docker_container_label_<label>: Docker container labels (.and-are substituted by_)__path__: Path on disk to the log file