Search code examples
dockerdocker-composegrafanasecret-keygrafana-loki

Hide docker driver parameters


Is it possible to hide docker logging driver options or at least load them from file?

I would like to commit docker-compose.yml to VCS but to mount LOKI_USER_ID and LOKI_API_KEY during runtime.

docker-compose.yml:

version: '3'

x-logging: &logging
  logging:
    driver: loki
    options:
      # \/\/\/
      loki-url: "https://${LOKI_USER_ID}:${LOKI_API_KEY}@logs-prod-us-central.grafana.net/loki/api/v1/push"
      # /\/\/\

services:

  service1:
    image: image1
    restart: always
    <<: *logging

  service2:
    image: image2
    restart: always
    depends_on:
      - service1
    <<: *logging

I've tried splitting them like:

logging.yml:

version: '3'

x-logging: &logging
  logging:
    driver: loki
    options:
      loki-url: "https://${LOKI_USER_ID}:${LOKI_API_KEY}@logs-prod-us-central.grafana.net/loki/api/v1/push"

docker-compose.yml:

version: '3'

services:

  service1:
    image: image1
    restart: always
    <<: *logging

  service2:
    image: image2
    restart: always
    depends_on:
      - service1
    <<: *logging

And then run

docker-compose -f logging.yml -f docker-compose.yml config

But it is unable to resolve the *logging alias:

ERROR: yaml.composer.ComposerError: found undefined alias 'logging'
  in "./docker-compose.yml", line 12, column 9

Which is pretty expected.


Another way of achieving this would to use envsubst and process docker-compose.yml file:

loki.env.sh:

export LOKI_USER_ID="XXX"
export LOKI_API_KEY="YYY"

docker-compose.yml:

version: '3'

x-logging: &logging
  logging:
    driver: loki
    options:
      loki-url: "https://${LOKI_USER_ID}:${LOKI_API_KEY}@logs-prod-us-central.grafana.net/loki/api/v1/push"

# ...

And launch it with:

source loki.env.sh
envsubst < docker-compose.yml | docker-compose up -d

Solution

  • Okay, so the solution is way simpler.

    According to the documentation docker-compose will utilize properties from .env file located in the same directory as docker-compose.yml is located in.

    .env:

    LOKI_USER_ID=XXX
    LOKI_API_KEY=YYY
    

    docker-compose.yml:

    version: '3'
    
    x-logging: &logging
      logging:
        driver: loki
        options:
          loki-url: "https://${LOKI_USER_ID}:${LOKI_API_KEY}@logs-prod-us-central.grafana.net/loki/api/v1/push"
    
    # ...
    

    And simply launch with

    docker-compose up