spring-boot: Unable to use Docker Compose support when mixing dedicated and shared services
I have a microservices-based mono-repo with multiple spring-boot applications. I would like to use Spring Boot Docker Compose support to start the dependent services. There are few services (ex: db) that are specific to each service and there are some services (ex: RabbitMQ) shared by multiple services.
All the microservices dependencies are defined in a single compose.yml fil. If I start any one microservice it starts all the services defined in the compose.yml file, which is ok for me.
version: "3.8"
services:
catalog-db:
image: postgres:16-alpine
...
orders-db:
image: postgres:16-alpine
...
rabbitmq:
image: rabbitmq:3.12.11-management
...
But the problem is, when I have multiple databases defined for different services, Flyway initialization fails.
Parameter 0 of method flyway in org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration required a single bean, but 2 were found:
- flywayConnectionDetailsForCatalogDb: defined in unknown location
- flywayConnectionDetailsForOrdersDb: defined in unknown location
Is there a way to configure at the microservice level (using spring.docker.compose.* properties) to take only certain services into account?
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16 (10 by maintainers)
Commits related to this issue
- Respect profiles when listing running Docker Compose containers Closes gh-40139 — committed to spring-projects/spring-boot by mhalbritter 2 years ago
--orphans=falsedid the trick. I’ve also added an integration test which tests that this indeed works.What could work is:
Run
docker compose --profile <profile> config --format=json. This returns the config for the services filtered by profile in JSON format:Then run
docker compose ps --format=json, which returns all running containers for the project (doesn’t do profile filtering):Now we can look in the config from above (under the
servicesobject) using thedocker compose psServicefield. If it’s not in the config, we totally ignore the running container.This way, when specifying e.g.
spring.docker.compose.profiles.active=orderit should only return running containers belonging to theorderprofile.@sach429 this looks like supporting profiles in docker ps through custom labels. I think https://github.com/docker/compose/issues/12361 is much more promis in because this means support for the entire docker compose ecosystem.
Another option would be to keep the services in one file, use Docker Compose profiles, and modify your
application.propertiesto control when individual services are started.This behavior is mentioned in the documentation:
There is some discussion on why we chose to implement things this way in https://github.com/spring-projects/spring-boot/issues/38398.
You can set
spring.docker.compose.lifecycle-management=noneto prevent this message. You will have to start the service manually (for example, withdocker compose up), but Spring Boot will create the service connections as expected.An additional mode like the one suggested in #39749, where
docker compose upwould be called without first checking to see what services are running, could help this situation. I will close this issue in favor of that issue.