docker-compose.yml紧凑的通用configuration

具有下面docker-compose.yml文件,其中包含n个具有相同configuration( imagevolumes等)的容器, command属性除外 。 有没有写没有重复? 那么如果我不得不改变一些configuration,我可以在一个地方做,而不是改变所有的事件。

一个select是使用image使用的variables,但是这不会解决问题,我想要添加另一个volume到所有的容器。

PS – 不在意如果需要更改为版本3。

 version: '2' services: container1: image: ${CONTAINER_IMAGE} user: my_user hostname: my_hostname command: unique running command for container1 env_file: - 'docker.env' volumes: - ./builds/image:/image - ~/workspace/component1:/component1 - ~/workspace/component2:/component2 links: - db:db - elastic-docker:elastic-docker - graphite:graphite-docker - zookeeper:zookeeper depends_on: - rabbitmq container2: image: ${CONTAINER_IMAGE} user: my_user hostname: my_hostname command: unique running command for container2 env_file: - 'docker.env' volumes: - ./builds/image:/image - ~/workspace/component1:/component1 - ~/workspace/component2:/component2 links: - db:db - elastic-docker:elastic-docker - graphite:graphite-docker - zookeeper:zookeeper depends_on: - rabbitmq container3: image: ${CONTAINER_IMAGE} user: my_user hostname: my_hostname command: unique running command for container3 env_file: - 'docker.env' volumes: - ./builds/image:/image - ~/workspace/component1:/component1 - ~/workspace/component2:/component2 links: - db:db - elastic-docker:elastic-docker - graphite:graphite-docker - zookeeper:zookeeper depends_on: - rabbitmq #and so on.. 

谢谢。

您可以使用extends关键字来重复使用第一个容器的configuration

 version: '2' services: c1: image: alpine command: echo 1 c2: extends: c1 command: echo 2 

或者使用第二个文件作为模板

 version: '2' services: template: image: alpine version: '2' services: c1: extends: file: template.yml service: template command: echo 1 

但是只有在绝对需要的情况下才能使用不同的configuration,因为如果没有您可以将其部署为服务,并使用docker service scale service=3或docker-compose(v3)

  deploy: mode: replicated replicas: 3 

你可以使用环境variables。 否则你真正需要的是一个模板引擎。 有许多模板引擎可用。 我曾经用过的一个是confd 。 你可以在下面的链接获得一个快速入门指南

https://github.com/kelseyhightower/confd/blob/master/docs/quick-start-guide.md

这将允许您使用tomlconfiguration文件循环并具有不同的卷映射和不同的容器数量。

甚至可以使用基于BASH的循环和脚本来生成组合文件。 但是维护你的文件会变得很困难。

PS:您可能感兴趣的一些链接:

https://theagileadmin.com/2015/11/12/templating-config-files-in-docker-containers/

http://steveadams.io/2016/08/18/Environment-Variable-Templates.html

http://tarunlalwani.com/post/simple-parameterized-config-files-docker/