There are 13 dockerized microservices in a project and I need to have one docker-compose.yml
file to run all the docker-compose.yml
files of every project. From what I googled, I found using a .env
seems to be the best practice. So this is what I do: Let’s have two docker-compose files, Project1.yml
and Project2.yml
as follows
project1.yml
version: '3'
services:
hello_world_a:
image: redis:latest
container_name: hw_a
hello_world_b:
image: redis:latest
container_name: hw_b
project2.yml
version: '3'
services:
hello_world_2:
image: redis:latest
container_name: hw_2
I created a docker-compose.yml
file as follows
version: '3'
services:
apps:
image: redis:latest
container_name: hw_c
env_file:
- .env
with the following .env
file
.env File
COMPOSE_PATH_SEPARATOR=:
COMPOSE_FILE=project1.yaml:docker-compose.yaml:project2.yaml
and inside the directory where docker-compose.yml
resides, I run docker compose up -d
and I get the following running containers
However, I need the docker-compose.yml
to be only a gateway to run other docker-compose files and I do not want this file to have any services. How can I do this?
update 1
To create an empty container I used scratch
image as follows
version: '3'
services:
apps:
build:
context: ./
dockerfile: DockerFile
container_name: apps
env_file:
- .env
and this is the DockerFile
FROM scratch
But I get an error
Error response from daemon: No command specified
update 2
Finally I changed the .env
as follows and it worked
COMPOSE_PATH_SEPARATOR=:
COMPOSE_FILE=project1.yaml:project2.yaml
2
Answers
Finally, I changed the .env as follows and it worked
You can also remove the
DockerFile
and simply writeFor the setup you’re showing, setting the
COMPOSE_FILE
environment variable is the important thing. The.env
file must be in the current directory, but if you explicitly setCOMPOSE_FILE
, you do not specifically need adocker-compose.yml
file in the directory as well.The last
.env
file in the question is enough; you do not need a Compose file with a dummy container:With this setup, the
.env
file contains control variables specific to Compose, and you do not need to pass it on to individual containers usingenv_file:
directives. I also might avoid explicitly settingcontainer_name:
to avoid potential conflicts. For larger projects there are also possible conflicts around manually-assignedports:
.Depending on your networking needs, I also might consider the merits of just starting all of the Compose projects separately. This will mean that each project’s containers have an automatically-assigned name prefix and you don’t need to specifically name the project in the container names. You may need to configure each service’s
default
network to point at a shared network with a known name.