Re-using environmental variables in docker-compose yml

你说的曾经没有我的故事 提交于 2020-02-12 07:17:34

问题


Is it possible to re-use environmental variables that are shared among multiple containers to avoid duplication, as illustrated in this example:

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 

回答1:


The extends option can be nice but it's not supported in 3.x compose files. Other ways to go are:

  1. Extension fields (compose file 3.4+)

    If you can use 3.4+ compose files, extension fields are probably the best option:

    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 directive

    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 in project root (or variables at actual compose environment)

    Variables from .env file can be referenced in service configuration:

    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
    



回答2:


You can use the extends directive to have multiple containers inherit the environment configuration from an underlying service description. For example, put the following in a file named base.yml:

version: '2'

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

Then in your 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

Then inside of container1, you will see:

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

And inside of container2 you will see:

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

You can obviously use extends for things other than the environment directive; it's a great way to avoid duplication when using docker-compose.




回答3:


You can reference local environment variables from within a docker-compose file. Assuming what you're wanting to do is make USER_NAME the same as DB_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}

Then, run docker-compose like:

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

Alternately, for something more permanent, and easier to type on a recurring basis:

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


来源:https://stackoverflow.com/questions/36283908/re-using-environmental-variables-in-docker-compose-yml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!