Inheritance or attachment using docker

The best way to start our application is to use the docker provided by us. The docker-compose command starts all services with the correct configuration.

Now we would like to provide a docker layout where the application works with another backend. In this compilation, 8 out of 10 services are the same, and 2 are different.

How to achieve this without code duplication? I see that a service can distribute a service from another file for docker linking, however, for this to happen, you still need to list all 10 services in both files.

+13
source share
2 answers

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

Assuming your source file is docker-compose.yaml , you can create 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 

... etc.

+11
source

With docker-compose 1.6 this should be possible.

Create docker-compose.yml with your shared services:

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

And the second docker-compose.prod.yml with your unique services:

 service03: image: image03 links: - service02 

Now you can start services 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

+10
source

Source: https://habr.com/ru/post/1274730/


All Articles