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
3
Answers
You can check
config-service
is healthy or not.Example bellow I check db healthy to run backend code:
Instead of simply depends you can add
So it looks like
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