在 docker-compose.yml 中重新使用环境变量

Re-using environment variables in docker-compose.yml

是否可以重复使用在多个容器之间共享的环境变量?

这个想法是为了避免重复,如本例所示:

version: '2'

services:

  db:
    image: example/db
    ports:
      - "8443:8443" 
    container_name: db
    hostname: db
    environment:
      - USER_NAME = admin 
      - USER_PASSWORD = admin 

svc:
  image: example/svc
  depends_on:
    - db
  ports:
    - "9443:9443"
  container_name: svc
  hostname: svc
  environment:
    - DB_URL = https://db:8443
    - DB_USER_NAME = admin
    - DB_USER_PASSWORD = admin 

您可以从 docker-compose 文件中引用本地环境变量。假设您想要做的是使 USER_NAMEDB_USER_NAME:

相同

docker-compose.yml

version: '2'

services:
  db:
    image: example/db
    ports:
      - "8443:8443" 
    container_name: db
    hostname: db
    environment:
      - USER_NAME = ${USERNAME}
      - USER_PASSWORD = ${PASSWORD}

svc:
  image: example/svc
  depends_on:
    - db
  ports:
    - "9443:9443"
  container_name: svc
  hostname: svc
  environment:
    - DB_URL = https://db:8443
    - DB_USER_NAME = ${USERNAME}
    - DB_USER_PASSWORD = ${PASSWORD}

然后,运行 docker-写成:

$ USERNAME="admin" PASSWORD="admin" docker-compose up

或者,对于更持久、更容易重复输入的内容:

$ printf '%s\n%s\n' 'export USERNAME="admin"' 'export PASSWORD="admin"' >> ~/.bash_profile
$ source ~/.bash_profile
$ docker-compose up

您可以使用 extends 指令(在组合 1.x2.x 中可用)让多个容器从底层服务描述继承 environment 配置。例如,将以下内容放入名为 base.yml 的文件中:

version: '2'

services:
  base:
    environment:
      DB_URL: https://db:8443
      DB_USER_NAME: admin
      DB_USER_PASSWORD: admin 

然后在你的 docker-compose.yml:

version: '2'

services:
  container1:
    image: alpine
    command: sh -c "env; sleep 900"
    extends:
      file: base.yml
      service: base

  container2:
    image: alpine
    command: sh -c "env; sleep 900"
    extends:
      file: base.yml
      service: base
    environment:
      ANOTHERVAR: this is a test

然后在container1里面,你会看到:

DB_URL=https://db:8443
DB_USER_NAME=admin
DB_USER_PASSWORD=admin

container2 里面你会看到:

DB_URL=https://db:8443
DB_USER_NAME=admin
DB_USER_PASSWORD=admin
ANOTHERVAR=this is a test

您显然可以将 extends 用于 environment 指令之外的其他内容;这是在使用 docker-compose.

时避免重复的好方法

extends 选项可能不错,但在 3.x 撰写文件中它是 not supported。其他方法是:

  1. Extension fields(撰写文件 3.4+)

    如果您可以使用 3.4+ 撰写文件,扩展字段可能是最佳选择:

    docker-compose.yml

     version: '3.4'
    
     x-common-variables: &common-variables
       VARIABLE: some_value
       ANOTHER_VARIABLE: another_value
    
     services:
       some_service:
         image: someimage
         environment: *common-variables
    
       another_service:
         image: anotherimage
         environment:
           <<: *common-variables
           NON_COMMON_VARIABLE: 'non_common_value'
    
  2. env_file 指令

    docker-compose.yml

     version: '3.2'
    
     services:
       some_service:
         image: someimage
         env_file:
           - 'variables.env'
    
       another_service:
         image: anotherimage
         env_file:
           - 'variables.env'
    

    variables.env

     VARIABLE=some_value
     ANOTHER_VARIABLE=another_value
    
  3. .env file 在项目根目录 (或实际撰写环境中的变量)

    .env 文件中的变量可以在服务配置中引用:

    docker-compose.yml

     version: '3.2'
    
     services:
       some_service:
         image: someimage
         environment:
           - VARIABLE
    
       another_service:
         image: anotherimage
         environment:
           - VARIABLE
           - ANOTHER_VARIABLE
    

    .env

     VARIABLE=some_value
     ANOTHER_VARIABLE=another_value