简体   繁体   English

在docker-compose中,是否可以在服务之间重用卷配置?

[英]In docker-compose, is it possible to reuse volume configuration between services?

I'm trying using a setup similar to the following: 我正在尝试使用类似于以下内容的设置:

version: '3.4'
x-my-volumes: &volumes
    - '../src:/var/www/src/:cached'
    - '../static:/var/www/static/:cached'
services:
  webserver:
    build: ./.docker/webserver
    volumes:
      - *volumes
      - './serverlogs/:/var/www/serverlogs/:delegated'
  node:
    build: ./.docker/node
    volumes:
      - *volumes

I set up the "shared" volumes in the x-my-volumes section and give it an anchor of "volumes", which I try to utilise in my services. 我在x-my-volumes部分中设置了“共享”卷,并给它提供了“ volumes”的锚点,我尝试将其用于服务中。 The main thing, though, is that the services have other volume mounts aside from the shared one. 不过,最主要的是,这些服务除了共享的服务外还具有其他卷安装。 This doesn't work, with errors such as " contains an invalid type, it should be an array " when doing a docker-compose up . 这不起作用,并且在进行docker-compose up时出现诸如“ contains an invalid type, it should be an array ”之contains an invalid type, it should be an array错误。

Is this possible for docker-compose? 这可能适用于docker-compose吗? I realise I can just copy and paste the volume mounts for each service in my real world scenario its 10+ services and 10+ volumes, so it's a lot of ugly duplication. 我意识到我可以在我的真实场景中为每个服务复制并粘贴其10个以上的服务和10个以上的卷的卷装载,因此这是很多丑陋的重复。

If the volume array is exactly the same between services, you can do this: 如果服务之间的卷阵列完全相同,则可以执行以下操作:

version: '3.4'

services:
  webserver:
    volumes: &volumes
      - one:one
      - two:two

  node:
    volumes: *volumes

If you wish to extend the array and add elements to it in some services, this does not seem to be supported in YAML as indicated by this GitHub issue and this StackOverflow question . 如果您希望扩展数组并在某些服务中向其添加元素,则此GitHub问题此StackOverflow问题指示,YAML中似乎不支持此功能


Perhaps to give you another approach to consider (although I am not sure it will be helpful for your use case): 也许可以给您另一种考虑的方法(尽管我不确定这对您的用例会有帮助):

In many of my docker compose, I define a "base" service, and then I can inherit some attributes of this service by other services, by using YAML merge. 在我的许多docker compose中,我定义了一个“基本”服务,然后可以通过使用YAML合并由其他服务继承该服务的某些属性。 Consider this example: 考虑以下示例:

version: '3'

services:
  bash:
    build: .
    entrypoint: /bin/bash
    <<: &default
      image: dannyben/borg-client
      volumes:
        - one:one
        - two:two

  init:
    <<: *default
    command: init -e repokey-blake2

  backup:
    <<: *default
    command: create --stats --progress ::initial-backup /borg/source

Using this approach I can have complex docker-compose files expressed in a very readable manner. 使用这种方法,我可以以非常易读的方式表达复杂的docker-compose文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM