Collecting Docker Container Logs and Pushing Them to Loki

  ·   3 min read

In the world of microservices and containerization, managing logs effectively is crucial for diagnosing issues and monitoring your applications. With the rise of various logging solutions, Loki by Grafana has emerged as a popular choice for aggregating logs from multiple services due to its lightweight and highly efficient design. In this article, I will walk you through the steps to collect Docker container logs and push them to Loki.

Prerequisites

Before we begin, ensure you have the following:

  1. Docker installed: Make sure you have Docker installed on your host machine. You can follow the official Docker installation guide if you haven’t done so.

  2. Loki running: You can run Loki locally using Docker as follows. Create a directory called loki and a configuration file loki-config.yml:

    server:
      http:
        port: 3100
    
    ingester:
      chunks:
        max_age: 1h
      lifecycler:
        join_after: 0s
        observe_period: 0s
    
    schema_config:
      configs:
        - from: 2020-10-01
          store: boltdb-shipper
          schema: v11
          index:
            prefix: index_
            period: 168h
    
    storage_config:
      boltdb_shipper:
        shared_store: filesystem
        active_index_directory: /loki/index
        cache_directory: /loki/cache
      filesystem:
        directory: /loki/chunks
    
  3. Docker Compose: For an easier setup of Loki, you can use Docker Compose. You can create a docker-compose.yml file as shown below:

    version: '3'
    
    services:
      loki:
        image: grafana/loki:latest
        volumes:
          - ./loki-config.yml:/etc/loki/loki-config.yml
          - loki-data:/loki
        command: ["/loki/loki", "-config.file=/etc/loki/loki-config.yml"]
        ports:
          - "3100:3100"
    
    volumes:
      loki-data:
    

Step 1: Running Loki

You can start Loki using Docker Compose by running:

docker-compose up -d

Verify that Loki is up by visiting http://localhost:3100/.

Step 2: Configure Docker Logging Driver

Docker supports several logging drivers that allow you to send logs directly to different destinations. For our case, you want to configure Docker to send logs to Loki using the loki driver. You can do this by editing the Docker daemon configuration file. It is typically located at /etc/docker/daemon.json. If the file doesn’t exist, create it with the following content:

{
  "log-driver": "loki",
  "log-opts": {
    "loki-url": "http://loki:3100/loki/api/v1/push"
  }
}

Make sure that your Docker containers can reach the Loki service, especially if they are in different Docker networks. Restart the Docker service to apply the changes:

sudo systemctl restart docker

Step 3: Running Docker Containers with Loki Logging Driver

Now, when you start Docker containers, you need to specify the logging driver. Here’s an example of running an Nginx container with the Loki logging driver:

docker run -d --name nginx --log-driver=loki nginx

You can verify that logs are being sent to Loki by checking Loki’s API endpoint:

curl -G http://localhost:3100/loki/api/v1/query --data-urlencode 'query={job="nginx"}'

Step 4: Visualizing Logs with Grafana

Grafana is an excellent tool to visualize and analyze logs. Start a new Grafana instance using Docker:

docker run -d -p 3000:3000 grafana/grafana

Now, log in to Grafana by visiting http://localhost:3000 (default credentials are admin/admin). Follow the steps below to configure Loki as a data source:

  1. Go to Configuration > Data Sources.
  2. Click on Add Data Source, search for and select Loki.
  3. Set the HTTP URL to http://loki:3100 and click on Save & Test.

You can now create dashboards and panels to visualize your Docker logs in Grafana.

Conclusion

In this article, we covered how to manage logging for Docker containers using Loki. By leveraging the Docker logging driver for Loki and visualizing logs in Grafana, you can maintain better operational awareness of your containerized applications.

Helpful Resources:

With these steps, you should now have a robust logging solution for your Docker containers. Happy logging!