Spring Boot Docker Compose support - vinhtbkit/bkit-kb GitHub Wiki
Spring Boot 3.1 got multiple noteworthy updates: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.1-Release-Notes
docker-compose up -d- Setup connection details in
application-local.propertiesor environment variables- Needs to remember the ports for each application
- Needs to manage when to start and when to stop the docker container
Dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
<optional>true</optional>
</dependency>
How it works
- Spring detects
docker-compose.ymlfiles. Path to the file can be configured - Call
docker compose up - Create service connection beans for each supported container
- Call
docker compose stopwhen application shutdown
Supported containers:
| Connection Details | Matched on |
|---|---|
| CassandraConnectionDetails | cassandra |
| ElasticsearchConnectionDetails | elasticsearch |
| JdbcConnectionDetails | Containers named "gvenzl/oracle-xe", "mariadb", "mssql/server", "mysql", or "postgres" |
| MongoConnectionDetails | mongo |
| R2dbcConnectionDetails | Containers named "gvenzl/oracle-xe", "mariadb", "mssql/server", "mysql", or "postgres" |
| RabbitConnectionDetails | rabbitmq |
| RedisConnectionDetails | redis |
| ZipkinConnectionDetails | openzipkin/zipkin |
If you need to change container name, use the labels:
services:
redis:
image: 'mycompany/mycustomredis:7.0'
ports:
- '6379'
labels:
org.springframework.boot.service-connection: redis
Lifecycle
- Default:
docker compose upwhen start,docker compose stopwhen shut down - Lifecycle can be configured to be either
- none: do not start or stop
- start-only
- start-and-stop
- Start and stop commands can also be configured: to be either
start/uporstop/down
spring.docker.compose.lifecycle-management=start-and-stop
spring.docker.compose.start.command=start
spring.docker.compose.stop.command=down
spring.docker.compose.stop.timeout=1m
Pros
- No need to specify the connection details. The
ConnectionDetailsbeans are created for you - Lifecycle is managed by Spring