2

I have three different projects, ProjectA depends on ProjectB, which in turn depends on ProjectC.

Assume you want to develop only ProjectC, so I want to use a setup with one container only at its runtime.

ProjectB needs ProjectC, so I have to define a docker-compose with two images.

ProjectA then again needs both ProjectB and ProjectC, so I fear I have to either duplicate a lot in the each docker-compose.yml file the longer the dependency chain gets.

I know I can link external images in a docker-compose.yml, yet this means more manual setup, as I have to checkout each project and run docker-compose.yml in each of them.

Basically I wonder how I can manage a docker-compose setup for a microservice architecture.

For the lack of a better phrase: Can I extend docker-compose.yml files?

kenorb
  • 155,785
  • 88
  • 678
  • 743
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
  • In short I think the answer is currently _no_. `extends` is very limited (see https://github.com/docker/compose/issues/1617 https://github.com/docker/compose/issues/3220) and there is no `includes`/ `path` or multi-file compose yet (see https://github.com/docker/compose/issues/318 and https://github.com/docker/compose/issues/3322) – KCD Jun 14 '16 at 22:53
  • Related: [How to extend service in Docker Compose V3?](https://stackoverflow.com/q/52587643/55075) – kenorb Mar 07 '19 at 22:36

3 Answers3

2

You can extend services in docker-compose.yml from another YAML file. Just use extends key in your docker-compose.yml.

For example:

projectC.yml

webapp:
 build: .
 environment:
   - KEY=VALUE
 ports:
   - "8000:8000"

projectB.yml

web:
  extends:
    file: projectC.yml
    service: webapp
Lauri
  • 4,336
  • 3
  • 18
  • 18
  • 1
    The current extend system seems to be quite restrictive as `services with 'links' cannot be extended`. [There's an issue for this on github](https://github.com/docker/compose/issues/1617). – k0pernikus Oct 09 '15 at 10:04
2

The next release (1.5.0) will support a way to extend a composition (see https://github.com/docker/compose/pull/2051). It is in master now if you want to try it out.

I think what you're describing is pretty close to this proposal https://github.com/docker/compose/issues/318

dnephin
  • 25,944
  • 9
  • 55
  • 45
0

The docker/compose issue 318 mentioned in 2016 in the comments is now (Aug. 2023) resolved.

It is completed by compose-spec/compose-spec PR 363, and detailed in Docker Desktop 4.22:

Split complex Compose projects into multiple subprojects with ‘include

If you’re working with complex applications, use the new include section in your Compose file to split your project into manageable subprojects.

Compared to merging files with CLI flags or using extends to share common attributes of a single service from another file, include loads external Compose files as self-contained building blocks, making it easier to collaborate on services across teams and share common dependency configurations within your organization.

For more on how you can try out this feature, read “Improve Docker Compose Modularity with include” from Nicolas De Loof, or refer to the documentation.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250