Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically add a service to docker compose project

I have a project with components base on Docker and orchestrated with docker-compose. Some of them are optional, and can be added at runtime.

I can think about two ways to achieve that:

  • Create a new serviceA.yml compose file and run it as a separate project
  • Add serviceA to my base compose.yml and run it again

What is the preferred option to do that?

I've also seen that you can combine docker-compose files with the extend keyword, but I don't think this can fit, since I have a variable number of services that I can add at runtime.

like image 851
teone Avatar asked Dec 11 '17 16:12

teone


1 Answers

I usually end up having multiple yml files.

Then you can add several -f flags to docker-compose command to indicate which services to run.

For instance, having:

  • docker-compose.yml: containing basic services
  • docker-compose-additional-services.yml: containing additional/optional services.

You can execute to start:

 docker-compose -f docker-compose.yml -f docker-compose-additional-services.yml up

And it will start all services.

Note, that all services are merged as if they were in a single file, so you can reference (depends_on, link) services from one file to the other.

like image 130
Gonzalo Matheu Avatar answered Sep 22 '22 05:09

Gonzalo Matheu