skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

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


  2. The simple workaround could be to define a nested YAML anchor like this:

    x-defaults: &my-defaults:
      volumes: &my-volumes
        - /first:/volume
        - /second:/volume
        
    services:  
      my-service1:
        <<: *my-defaults
        volumes:
          <<: *my-volumes
          - /additional:/volume
         
      my-service2:
        <<: *my-defaults
        volumes:
          <<: *my-volumes
          - /custom:/vol
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search