services:
  loki:
    image: grafana/loki:2.8.2
    container_name: loki-observability
    ports:
      - "3100:3100"
    command: -config.file=/etc/loki/local-config.yaml
    configs:
      - source: loki_config
        target: /etc/loki/local-config.yaml
    volumes:
      - loki-wal:/wal
      - loki-chunks:/loki/chunks
      - loki-index:/loki/index
    networks:
      - observability_observability

  promtail-config-init:
    image: busybox:latest
    container_name: promtail-config-init
    volumes:
      - promtail-config-vol:/config
    command:
      - /bin/sh
      - -c
      - |
        cat > /config/config.yaml <<'EOF'
        server:
          http_listen_port: 9080
          grpc_listen_port: 0
        positions:
          filename: /tmp/positions.yaml
        clients:
          - url: http://loki:3100/loki/api/v1/push
        scrape_configs:
          - job_name: docker_logs
            static_configs:
              - targets: [localhost]
                labels:
                  job: docker
                  __path__: /var/lib/docker/containers/*/*.log
          - job_name: system_logs
            static_configs:
              - targets: [localhost]
                labels:
                  job: syslog
                  __path__: /var/log/*.log
        EOF
        echo "Promtail config created"
    restart: "no"
    networks:
      - observability_observability

  promtail:
    image: grafana/promtail:2.8.2
    container_name: promtail-observability
    volumes:
      - /var/lib/docker/containers:/var/lib/docker/containers:ro
      - /var/log:/var/log:ro
      - promtail-config-vol:/etc/promtail:ro
    command:
      - -config.file=/etc/promtail/config.yaml
    networks:
      - observability_observability
    depends_on:
      - promtail-config-init

configs:
  loki_config:
    content: |
      auth_enabled: false
      server:
        http_listen_port: 3100
        grpc_listen_port: 9096
      common:
        path_prefix: /loki
        storage:
          filesystem:
            chunks_directory: /loki/chunks
            rules_directory: /loki/rules
        replication_factor: 1
        ring:
          instance_addr: 127.0.0.1
          kvstore:
            store: inmemory
      ingester:
        wal:
          enabled: true
          dir: /wal
          flush_on_shutdown: true
        chunk_idle_period: 5m
        chunk_retain_period: 30s
        max_chunk_age: 1h
        lifecycler:
          ring:
            replication_factor: 1
      schema_config:
        configs:
          - from: 2020-10-24
            store: boltdb-shipper
            object_store: filesystem
            schema: v11
            index:
              prefix: index_
              period: 24h
      storage_config:
        boltdb_shipper:
          active_index_directory: /loki/index
          cache_location: /loki/index
          shared_store: filesystem
        filesystem:
          directory: /loki/chunks
      limits_config:
        enforce_metric_name: false
        reject_old_samples: true
        reject_old_samples_max_age: 168h
        ingestion_rate_mb: 10
        ingestion_burst_size_mb: 20
      compactor:
        working_directory: /loki/compactor
        shared_store: filesystem
        compaction_interval: 10m

volumes:
  promtail-config-vol:
  loki-wal:
  loki-chunks:
  loki-index:

networks:
  observability_observability:
    external: true
