split huge docker-compose file contains fragment into multiiple

27 Views Asked by At

I have huge docker-compose file containing fragments and anchors like this:

version: '3'

x-common-repo-volumes:
 - &source-bin /path/lib/:/work/lib/
 - ...
 - ...

services:    
  foobar:
    image: ...
    volumes:
      - *source-bin
      - ...
      - ...

I want to split all fragments to separate file named comon.yml :

version: '3'

x-common-repo-volumes:
 - &source-bin /path/lib/:/work/lib/
 - ...
 - ...

and real services into main.yml (there are also more yml files that shares the same fragments like second.yml)

services:    
  foobar:
    image: ...
    volumes:
      - *source-bin
      - ...
      - ...

When I try to run this two file with docker compose -f like this:

docker compose -f common.yml -f main.yml up d

I get this error:

unknown anchor 'source-bin' referenced

what is wrong here and How can I split huge docker-compose file containing so many anchors and fragments to multiple ones so I can import the shared fragments into all of splitted services files?

NOTE: I've already found similar issue on github not sure using pipe (|) is good idea or not here

1

There are 1 best solutions below

0
Francisco Brito On

I was investigating this interesting question and it seems unfortunately it is not possible to make anchor references between different compose files. I attach a link where they apparently confirm that it is not possible.

P.S: There is a possible solution that user @Aradiv shared in a github issue, but I honestly don't know if it will work for your case, I'll leave the link anyway.