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.properties
or 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.yml
files. Path to the file can be configured - Call
docker compose up
- Create service connection beans for each supported container
- Call
docker compose stop
when 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 up
when start,docker compose stop
when 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/up
orstop/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
ConnectionDetails
beans are created for you - Lifecycle is managed by Spring