How to Leverage Spring Boot 3.1’s Built‑In Docker Compose Support
Spring Boot 3.1 introduces an enhanced Docker Compose module that automatically starts and stops containers, maps service properties without manual duplication, and configures application settings like Redis and MySQL, allowing developers to quickly integrate Docker Compose files into their Spring applications with minimal setup.
This article introduces the newly released Spring Boot 3.1 version, focusing on the ability to interact directly with Spring Boot applications using Docker Compose files.
Although not entirely new, the feature has significant improvements: the Spring Boot Docker Compose module can automatically start and stop your containers, and you no longer need to copy properties between Docker Compose files and application.properties.
In this blog post, I will demonstrate how to get started with this exciting new feature in Spring Boot 3.1.
Quick Start
First, use Spring Initializr to set up a basic Spring Boot 3.1 project.
Maven project
Version 3.1.0
Java 17
Dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
<optional>true</optional>
</dependency>
</dependencies>spring-boot-docker-compose Introduction
Core functions of spring-boot-docker-compose :
Assist managing (start, stop) Docker Compose services
Read docker-compose file and automatically configure application.yml
After Docker Compose starts, spring-boot-docker-compose automatically discovers the services defined in the compose file and configures the corresponding Spring properties, such as spring.redis.host and spring.redis.port for Redis, or spring.datasource.url, spring.datasource.username, and spring.datasource.password for MySQL. Existing settings in application.properties are overridden to ensure the application connects to the Docker Compose services.
To resolve port conflicts, you only need to expose ports in docker-compose.yml without explicit mappings; Spring Boot will use random ports and automatically detect and configure them. An example docker-compose.yml file:
version: "3"
services:
mysql:
image: mysql:8.0.33
ports:
- "3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: pigxCurrently Supported Types
Elasticsearch
MariaDB with JDBC and R2DBC
MongoDB
MySQL with JDBC and R2DBC
PostgreSQL with JDBC and R2DBC
RabbitMQ
Redis
ZipkinSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
