在Yaml中合并变量

时间:2020-08-18 13:05:58

标签: docker docker-compose yaml

我正在写我的docker compose文件,我想对其进行参数化。 但是我不想使用.env文件(已经用于传递密钥)。 初始文件:

version: '3.7'
services:
  service1:
    image: image1
    environment:
      PORT: '5000'
      GOOGLE_APPLICATION_CREDENTIALS: path_to_creds
    expose:
      - 5000
  service2:
    image: image2
    ports:
      - "${SERVER_IP:-127.0.0.1}:80:8050"
    environment:
      PORT: '8050'
  service3:
    image: image2
    ports:
      - "${SERVER_IP:-127.0.0.1}:80:8050"
    environment:
      PORT: '8050'
      GOOGLE_APPLICATION_CREDENTIALS: path_to_creds

this page之后,我制作了这个docker-compose.yaml:

version: '3.7'

# an app
x-api: &a1
  image: image1
  environment:
    PORT: '5000'
  expose:
    - 5000

# another
x-web: &a2
  image: image2
  ports:
    - "${SERVER_IP:-127.0.0.1}:80:8050"
  environment:
    PORT: '8050'

# some more envs
x-gcp: &gcp
  environment:
    GOOGLE_APPLICATION_CREDENTIALS: path_to_creds


services:
  service1:
    <<: [*gcp, *a1]
  service2:
    <<: *a2
  service3:
    <<: [*gcp, *a2]

但是这个enviromnent键有一个问题: 简而言之:

A: &a
    k:
    a: 1

B: &b
    k:
    b: 2

C:
    <<: [*b, *a]

结果:

C:
  k:
    b: 2

而不是:

C:
  k:
    a: 1
    b: 2

0 个答案:

没有答案