Is it possible to "import" or link a docker-compose file into another docker-compose file?
Suppose I have two files:
# docker-compose-1.yml
services:
A:
# config
B:
# config
# docker-compose-2.yml
services:
C:
# config
import: docker-compose-1.yml
By running docker-compose -f docker-compose-2.yml up
, I would like to start containers A, B (specified in the imported file) and C. Is this possible, without having to link both files with the -f
parameter?
By extending
It's possible to extend or use multiple docker-compose files and their services and link them in just one file. You can take a look at this link to understand how is the other usages of multiple compose files. But you can't include the file yet without link related files together as you mentioned.
Here I defined a common-services.yaml:
And then I created a docker-compose.yml and included the common-services.yml file and its own service as well.
By .env technique
And if you want to avoid chaining usage of multiple files there is also a technique with .env files. I will rewrite the previous example with .env technique.
Let's add another service as an example in the common-services.yml
And Finally, load all of them in the docker-compose file without even mention to those services.
In the end, you will have three running services.