我正在写我的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