0

是否可以隐藏 docker 日志记录驱动程序选项或至少从文件中加载它们?

我想提交docker-compose.ymlVCS,但要LOKI_USER_IDLOKI_API_KEY运行时挂载。

码头工人-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

我试过像这样拆分它们:

日志记录.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"

码头工人-compose.yml:

version: '3'

services:

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

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

然后运行

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

但它无法解析*logging别名:

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

这是非常值得期待的。


实现此目的的另一种方法是使用envsubst和处理docker-compose.yml文件:

loki.env.sh:

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

码头工人-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"

# ...

并使用以下命令启动它:

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

1 回答 1

1

好的,所以解决方案更简单。

根据文档, docker-compose 将利用.env位于与所在目录相同的文件中的属性docker-compose.yml

.env:

LOKI_USER_ID=XXX
LOKI_API_KEY=YYY

码头工人-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"

# ...

只需启动

docker-compose up
于 2021-04-13T16:04:24.763 回答