skip to Main Content

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

enter image description here

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


  1. Chosen as BEST ANSWER

    Finally, I changed the .env as follows and it worked

    COMPOSE_PATH_SEPARATOR=:
    COMPOSE_FILE=project1.yaml:project2.yaml
    

    You can also remove the DockerFile and simply write

    version: '3'
    services:
      apps:
        image: scratch
        container_name: apps
        env_file:
          - .env
    

  2. For 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 set COMPOSE_FILE, you do not specifically need a docker-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:

    COMPOSE_PATH_SEPARATOR=:
    COMPOSE_FILE=project1.yaml:project2.yaml
    

    With this setup, the .env file contains control variables specific to Compose, and you do not need to pass it on to individual containers using env_file: directives. I also might avoid explicitly setting container_name: to avoid potential conflicts. For larger projects there are also possible conflicts around manually-assigned ports:.

    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.

    # project1/docker-compose.yml
    version: '3.8'
    services:
      a:
        image: redis:latest
        # ports: ['6379']  # if required; to find the host port
        #                  # `docker-compose port a 6379`
        # no container_name: or env_file:
    networks:
      default:
        name: project
        external: true
    
    docker network create project
    for d in project*; do
      (cd "$d" && docker-compose up -d)
    done
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search