Master Spring Boot 3 Docker Compose: 128 Real‑World Cases & Step‑by‑Step Guide
Explore a comprehensive collection of 128 Spring Boot 3 practical examples demonstrating Docker Compose integration, from environment setup and dependency configuration to service lifecycle management, custom compose files, container persistence, and advanced features, enabling developers to effortlessly run MySQL, Redis, and other services without manual configuration.
Introduction
Spring Boot 3.2.5 now includes a Docker Compose module that simplifies the definition and management of dependent services such as MySQL and Redis.
Prerequisites
Install Docker Desktop (Windows) and ensure WSL is updated with wsl --update .
Dependency Management
Add the Docker Compose starter as an optional dependency:
<code><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
<optional>true</optional>
</dependency>
</code>When the dependency is present Spring Boot will:
Search the application directory for compose.yml and related files.
Invoke docker compose using the discovered file.
Create a service‑connection bean for each supported container.
Call docker compose stop when the application shuts down.
Creating a Project
Select MySQL, Redis and Docker Compose during project generation. Spring Boot automatically creates a compose.yml with MySQL and Redis services.
<code>services:
mysql:
image: 'mysql:latest'
environment:
- 'MYSQL_DATABASE=mydatabase'
- 'MYSQL_PASSWORD=secret'
- 'MYSQL_ROOT_PASSWORD=verysecret'
- 'MYSQL_USER=myuser'
ports:
- '3306'
redis:
image: 'redis:latest'
ports:
- '6379'
</code>Running the application pulls the images, starts the containers, and logs show successful connections.
Database Operations
Even without explicit configuration, the application can use the automatically created beans. Example entity, repository, service and controller:
<code>@Entity
@Table(name = "t_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// getters and setters
}
public interface UserRepository extends JpaRepository<User, Long> {}
@Service
public class UserService {
private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Transactional
public User save(User user) {
return this.userRepository.saveAndFlush(user);
}
public List<User> query() {
return this.userRepository.findAll();
}
}
@RestController
@RequestMapping("/users")
public class UserController {
@Resource
private UserService userService;
@PostMapping("")
public User save(@RequestBody User user) {
return this.userService.save(user);
}
@GetMapping("")
public List<User> list() {
return this.userService.query();
}
}
</code>Advanced Configuration
Custom Compose File
<code>spring:
docker:
compose:
file: "../my-compose.yml"
</code>Lifecycle Management
Control when Docker Compose starts and stops with spring.docker.compose.lifecycle-management . Supported values are none , start-only , and start-and-stop .
<code>spring:
docker:
compose:
lifecycle-management: start-and-stop
start:
command: start
stop:
command: down
timeout: 1m
</code>Container Persistence
Persist MySQL data by mounting a host volume:
<code>services:
mysql:
image: 'mysql:latest'
command: --lower_case_table_names=1
environment:
- 'MYSQL_DATABASE=demo'
- 'MYSQL_ROOT_PASSWORD=123123'
ports:
- '3308:3306'
volumes:
- 'd:/mysql/data:/var/lib/mysql'
</code>Ignoring a Container
Prevent Spring Boot from connecting to a specific container by adding the label org.springframework.boot.ignore=true :
<code>services:
mysql:
image: 'mysql:latest'
labels:
'org.springframework.boot.ignore': true
</code>These examples demonstrate how Spring Boot 3 can automatically manage Docker Compose services, reducing boilerplate configuration and streamlining development workflows.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
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.