Search code examples
dockermergeyaml

docker compose merging yaml files fails with conflicting services


I want merge 2 docker compose and I get some errors:

common/compose.yaml

version: '3.9'

services:
  hello-world:
    image: hello-world

prod/compose.yaml

include:
  - ../common/compose.yaml
services:
  hello-world:
    environment:
      - name=value 

Then I try to start the container with:

prod/compose.yaml: docker compose up

I get the error: Imported compose file [common/compose.yaml] defines conflicting service hello-world

If I remove the service section it works. Why is the service not merged? This should work in docker compose > 2.20.


Solution

  • According to the Docker documentation for the include keyword, it seems it wasn't intended for this use:

    "Compose displays a warning if resource names conflict and doesn't try to merge them."

    You may need to instead resort to running docker compose -f common/compose.yaml -f prod/compose.yaml as per the example provided in the official documentation.