skip to Main Content

I am trying to set up a main docker-compose file that contains things like database, firebase auth, pubsub emulator etc. This main file is in the parent directory and there are docker-compose files in each sub-folder. I am following a microservices architecture but don’t want a single large compose file since the number of microservices is more than 50 and it ends up using a lot of resources. So I have managed to create the files under the same docker network but the issue I am facing now is that I cannot use the name of a service in the parent folder docker-compose file inside the child folder docker-compose file.

Here is an example of docker-compose service in parent folder:

  postgres_db:
    image: postgres:latest
    restart: always
    environment:
      - POSTGRES_PASSWORD=postgrespw
    ports:
      - "5432:5432"
    volumes:
      - postgres:/var/lib/postgresql/data

And here is an example of docker-compose service in child folder:

  service_1:
    build:
      context: ../../
      dockerfile: ./services/service_1/Dockerfile.dev
      target: development
    restart: always
    environment:
      - DATABASE_URL=postgresql+psycopg2://postgres:postgrespw@postgres_db:5432/localdb
      - PORT=8080
    ports:
      - "8080:8080"
    volumes:
      - ../../lib/:/app/lib
      - ../../services/service_1/:/app
    depends_on:
      - postgres_db

This throws the following error:

service "service_1" depends on undefined service postgres_db: invalid
compose project

I need to be able to use the service name of a parent docker-compose file inside the child docker-compose file.

2

Answers


  1. depends_on purpose is to handle container dependencies in a single compose file and bring up containers in order based on their dependencies.

    You can not use them between your compose files.

    You need to handle it in your application flow or write an entrypoint script for your containers, which checks backing services your application depends on, are accessible before starting your application.

    Login or Signup to reply.
  2. You can’t reference info from another compose file. The "depends_on" has to be a service that’s referenced in the same file.

    If you’re trying to run subsections of your stack in compose, you might want to try the "profiles" feature of docker compose. The compose file will still be large, but you won’t have to run all the defined services at once.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search