Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Docker compose service priority

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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"]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading