跨容器重用配置

时间:2019-10-16 17:06:29

标签: docker docker-compose config

通过配置并将其重新用于docker容器的行业惯例是什么,一些配置是常见的,而有些则具有自定义值。例如。

通知产品容器具有自定义配置参数。 My_CUSTOM_CONFIG

customer:
  image: node
  build:
    context: ./Customer
    dockerfile: Dockerfile
  environment:
    DATABASE_HOST: database
    DATABASE_NAME: MyDB
    DATABASE_USER: myusername
    DATABASE_PASSWORD: mypassword
    SERVICE_PORT: 80
  depends_on:
    - database

product:
  image: node
  container_name: product
  restart: on-failure
  build:
    context: ./Product
    dockerfile: Dockerfile
  environment:
    DATABASE_HOST: database
    DATABASE_NAME: MyDB
    DATABASE_USER: myusername
    DATABASE_PASSWORD: mypassword
    SERVICE_PORT: 80
    My_CUSTOM_CONFIG: secret
  depends_on:
    - consul
    - database

enter image description here

1 个答案:

答案 0 :(得分:0)

看看env_file

  

从文件添加环境变量。可以是单个值或列表。

将其放入.env

DATABASE_HOST=database
DATABASE_NAME=MyDB
DATABASE_USER=myusername
DATABASE_PASSWORD=mypassword
SERVICE_PORT=80

然后在docker-compose.yml

中使用它
customer:
  image: node
  build:
    context: ./Customer
    dockerfile: Dockerfile
  env_file: .env
  depends_on:
    - database

product:
  image: node
  container_name: product
  restart: on-failure
  build:
    context: ./Product
    dockerfile: Dockerfile
  env_file: .env
  environment:
    My_CUSTOM_CONFIG: secret
  depends_on:
    - consul
    - database