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.
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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With