0

I have a project with Spring Boot as a microservice. In Docker Compose, when the config service is executed, the other services must be executed. I have done this with depend on. But sometimes other services come up without the config service coming up. Is there a standard solution to have the second service come up after the config and not before it? this is my sample docker-compose.yml:

`version: "3.9"
services:
  config-service:
    image: 'config-service:v1-BETA'
    build: '/opt/core/config-service/.'
    container_name: 'config-service'
    restart: always
    volumes:
      - db-data:/opt/core/data
    networks:
      - core-network
    ports:
      - "8088:8088"


  discovery-service:
    image: 'discovery-service:v1-BETA'
    build: '/opt/core/discovery-service/.'
    container_name: 'discovery-service'
    depends_on:
      - config-service
    ports:
      - "8061:8061"
    restart: always
    volumes:
      - db-data:/opt/core/data
    networks:
      - core-network
    environment:
      - "SPRING_PROFILES_ACTIVE=prod"

I also added server resources and tried to find a way like sleep function but failed

  • Are you looking for one of the solutions in [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y)? – David Maze Jul 12 '23 at 10:37

3 Answers3

1

Instead of simply depends you can add

 depends_on:
      config-service:
        condition: service_started

So it looks like

`version: "3.9"
services:
  config-service:
    image: 'config-service:v1-BETA'
    build: '/opt/core/config-service/.'
    container_name: 'config-service'
    restart: always
    volumes:
      - db-data:/opt/core/data
    networks:
      - core-network
    ports:
      - "8088:8088"


  discovery-service:
    image: 'discovery-service:v1-BETA'
    build: '/opt/core/discovery-service/.'
    container_name: 'discovery-service'
    depends_on:
      config-service:
        condition: service_started
    ports:
      - "8061:8061"
    restart: always
    volumes:
      - db-data:/opt/core/data
    networks:
      - core-network
    environment:
      - "SPRING_PROFILES_ACTIVE=prod"
Neil
  • 164
  • 11
0

You can check config-service is healthy or not. Example bellow I check db healthy to run backend code:

version: '3.8'

services:

  db:
    container_name: db
    image: postgres:15-alpine
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready" ]
      interval: 1m30s
      timeout: 30s
      retries: 5
      start_period: 30s
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=blogs

  fastapi:
    container_name: fastapi
    ports:
      - "8000:8000"
    depends_on:
      db:
        condition: service_healthy
PaPu
  • 731
  • 7
  • 12
0

In Docker Compose, the depends_on directive does not guarantee ordering service startup. It only waits for the specified services to be started!

Maybe a good solution would be to run a script that takes care not to build the second service until the first one is up! On solution that I found is here

I have already fixed this problem using this solution. Copy the end of the script to the main project folder. Write Docker Compose like this

version: "3.9"
services:
  config-service:
    image: 'config-service:v1-BETA'
    build: '/opt/core/config-service/.'
    container_name: 'config-service'
    restart: always
    volumes:
      - db-data:/opt/core/data
    networks:
      - core-network
    ports:
      - "8088:8088"

  discovery-service:
    image: 'discovery-service:v1-BETA'
    build: '/opt/core/discovery-service/.'
    container_name: 'discovery-service'
    depends_on:
      - config-service
    ports:
      - "8061:8061"
    restart: always
    volumes:
      - db-data:/opt/core/data
    networks:
      - core-network
    environment:
      - "SPRING_PROFILES_ACTIVE=prod"
    command: ["dockerize", "-wait", "tcp://config-service:8088", "-timeout", "30s", "java", "-jar", "your-application.jar"]
Soheil Babadi
  • 562
  • 2
  • 4
  • 15