隐藏 docker 驱动程序参数

Hide docker driver parameters

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

我想将 docker-compose.yml 提交到 VCS,但要在 运行 时间内挂载 LOKI_USER_IDLOKI_API_KEY

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

我试过像这样拆分它们:

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

然后运行

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"

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"

# ...

并启动它:

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

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

根据 documentation docker-compose 将利用与 docker-compose.yml 位于同一目录的 .env 文件的属性。

.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"

# ...

只需使用

启动
docker-compose up