Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance or nesting with docker compose

The best way to start our application is by using a docker compose we provide. The docker-compose starts all the services with the right configuration.

Now we would like to provide a docker-compose where the application runs with a different backend. In this compose 8 out 10 services are the same and 2 are different.

How to achieve this without code duplication? I see that a service can extend a service from another docker-compose file, however this would still require to list all the 10 services in both files.

like image 580
Reto Gmür Avatar asked Oct 29 '15 07:10

Reto Gmür


People also ask

Does Docker compose inherit environment variables?

Using docker-compose , you can inherit env variables in docker-compose. yml and subsequently any Dockerfile(s) called by docker-compose to build images. This is useful when the Dockerfile RUN command should execute commands specific to the environment.

How do I handle multiple files in Docker compose?

To use multiple override files, or an override file with a different name, you can pass the -f option to the docker-compose up command. The base Compose file has to be passed on the first position of the command.

Is Docker compose deprecated?

Following the deprecation of Compose on Kubernetes, support for Kubernetes in the stack and context commands in the docker CLI is now marked as deprecated as well.

Can you have two Docker compose files?

Using Multiple Docker Compose Files Use multiple Docker Compose files when you want to change your app for different environments (e.g., dev, staging, and production) or when you want to run admin tasks against a Compose application.


2 Answers

With docker-compose 1.6 this should be possible.

Create a docker-compose.yml with your common services:

service01:   image: image01   links:     - service02  service02:   image: image02 

And a second file, docker-compose.prod.yml with your unique services:

service03:   image: image03   links:     - service02 

Now you can start service 01, 02 and 03 with this command:

docker-compose -f docker-compose.yml -f docker-compose.prod.yml 

For more information, see the official documentation: https://docs.docker.com/compose/extends/#multiple-compose-files

like image 132
José Luis Avatar answered Sep 18 '22 12:09

José Luis


The easiest way to achieve this is to create a second compose file. In the second file, you can use the extend feature of Docker Compose which allows you to "inherit" services from another file: https://docs.docker.com/compose/extends/

Assuming your original file is docker-compose.yaml, you could create a swap-backend-compose.yaml:

service-one:   extends:     file: docker-compose.yaml     service: service-one  service-two:   extends:     file: docker-compose.yaml     service: service-two   environment:     - BACKEND=some_other_value 

...and so on.

like image 39
Justin Niessner Avatar answered Sep 21 '22 12:09

Justin Niessner