Configuring Docker Logging Driver for Loki
Docker allows you to use Loki as a logging driver to send container logs directly to your Loki instance. This guide explains how to configure the Docker logging driver for Loki.
1. Changing the Default Logging Driver
To configure Docker to use Loki as the default logging driver, you need to update the Docker daemon configuration.
Step 1: Update the /etc/docker/daemon.json File
Create or edit the Docker daemon configuration file at /etc/docker/daemon.json. Add the following configuration:
Basic Configuration:
{
"debug": true,
"log-driver": "loki"
}
This sets Loki as the default logging driver and enables debugging for Docker.
2. Sending Logs to Loki Cloud (Grafana)
To send container logs to a Loki cloud instance in Grafana, you need to configure additional options.
Example Configuration:
{
"debug": true,
"log-driver": "loki",
"log-opts": {
"loki-url": "https://<user_id>:<password>@logs-us-west1.grafana.net/loki/api/v1/push",
"loki-batch-size": "400"
}
}
Explanation of Configuration Options:
loki-url: The URL of your Loki instance, including the user ID and password. Replace<user_id>and<password>with your credentials.loki-batch-size: Specifies the batch size for log messages (default is400). Adjust this based on your workload.
3. Restart Docker
After updating the configuration file, restart the Docker daemon to apply the changes:
sudo systemctl restart docker
4. Verify the Configuration
To ensure the logging driver is set to Loki, run the following command:
docker info | grep "Logging Driver"
The output should show loki as the logging driver.
5. Setting Logging Driver Per Container (Optional)
If you don’t want to set Loki as the default logging driver for all containers, you can configure it on a per-container basis:
docker run --log-driver=loki --log-opt loki-url="https://<user_id>:<password>@logs-us-west1.grafana.net/loki/api/v1/push" my-container
- Ensure that the Loki URL is correct and reachable from the Docker host.
- Replace sensitive credentials (e.g.,
<user_id>and<password>) with appropriate secrets or environment variables for better security.