WittCodeπŸ’»

Dockerizing Loki

By

Learn how to get a Loki instance up and running quickly with Docker. We will go over configuring Loki, how to persist data, and how to interact with Loki using the Loki HTTP API.

Table of Contents πŸ“–

Spinning Up a Container

We can spin up a Loki Docker container by using the grafana/loki image.

docker run --name=loki -p 3100:3100 grafana/loki

INFO: By default Loki runs on port 3100.

The Loki image is based on the Alpine Linux distribution. Because of this, the image is relatively small.

docker exec -it loki cat /proc/version
Linux version 6.10.0-linuxkit (root@buildkitsandbox) (gcc (Alpine 13.2.1_git20240309) 13.2.1 20240309, GNU ld (GNU Binutils) 2.42) #1 SMP Wed Jul 17 10:51:09 UTC 2024

docker ps -s 
CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS                PORTS                              NAMES               SIZE
b15a31be2075   grafana/loki        "/usr/bin/loki -conf…"   11 minutes ago   Up 11 minutes         0.0.0.0:3100->3100/tcp             loki                312B (virtual 77MB)

Querying Loki

Currently Loki does not provide a UI of its own. However, Loki does expose an HTTP API for pushing and querying logs.

INFO: To get a handy UI for Loki, look into hooking it up to Grafana.

https://grafana.com/docs/loki/latest/reference/loki-http-api/
# Get the log level
curl localhost:3100/log_level
{"message":"Current log level is info"}

# Get Loki build information
curl localhost:3100/loki/api/v1/status
/buildinfo
{"version":"2.9.10","revision":"7664eda07b","branch":"HEAD","buildUser":"root@ce8cd5f6be4c","buildDate":"2024-08-09T18:28:38Z","goVersion":""}

# Send log entries to Loki
curl -X POST localhost:3100/loki/api/v1/push

Configuring Loki

Loki's configuration file is located at /etc/loki/local-config.yaml.

docker exec -it loki cat /etc/loki/local-config.yaml

auth_enabled: false

server:
  http_listen_port: 3100

common:
  path_prefix: /loki
  storage:
    filesystem:
      chunks_directory: /loki/chunks
      rules_directory: /loki/rules
  replication_factor: 1
  ring:
    kvstore:
      store: inmemory

schema_config:
  configs:
    - from: 2020-10-24
      store: boltdb-shipper
      object_store: filesystem
      schema: v11
      index:
        prefix: index_
        period: 24h

ruler:
  alertmanager_url: http://localhost:9093
  • auth_enabled - Enables authentication through the X-Scope-OrgID header, which must be present if true.
  • server - Configures the server.
  • http_listen_port - The port to listen on. Default is 3100.
  • common - Common configuration to be shared between multiple modules.
  • storage - Where to store the logs. Can store logs in S3, GCS, filesystem, etc.
  • filesystem - Locations to store chunks and rules in.
  • alertmanager_url - Comma-separated list of URLs to send notifications to.

We can provide a custom Loki configuration by replacing this configuration file with our own.

docker run -d --name=loki -p 3100:3100 -v loki.yaml:/etc/loki/local-config.yaml grafana/loki

Persisting Data

We can persist data in a Loki container by adding a Docker volume to the /loki directory.

docker volume create loki_data
loki_data
docker run -d --name=loki -p 3100:3100 -v loki_data:/loki grafana/loki