'How to inherit/merge volumes from yaml anchors in docker-compose?

I want my docker-compose file to merge (reuse) volumes definitions, as follows:

x-defaults: &my-defaults:
  volumes:
    - /first:/volume
    - /second:/volume

services:  
  my-service1:
    <<: *my-defaults
    volumes:
      - /additional:/volume
 
  my-service2:
    <<: *my-defaults
    volumes:
      - /custom:/vol

Result: only the /additional:/volume is mapped.

Question: how can I achieve a real merge here?



Solution 1:[1]

Simple answer: it's not possible. Array lists are replaced on inheritance, thus when overriding, have to repeat all list statements.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 membersound