Backend Development 8 min read

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.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master Spring Boot 3 Docker Compose: 128 Real‑World Cases & Step‑by‑Step Guide

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 .

Docker Desktop download page
Docker Desktop download page

Dependency Management

Add the Docker Compose starter as an optional dependency:

<code>&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-docker-compose&lt;/artifactId&gt;
  &lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;
</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.

Spring Initializr project selection with MySQL, Redis, Docker Compose
Spring Initializr project selection with MySQL, Redis, Docker Compose
<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.

Application logs showing MySQL connection
Application logs showing MySQL connection
Docker container list with MySQL and Redis
Docker container list with MySQL and Redis

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>
Postman test of the User API
Postman test of the User API
Query interface result
Query interface result

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>
Supported Docker Compose connections diagram
Supported Docker Compose connections diagram

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.

JavaMicroservicesbackend developmentSpring BootDocker Compose
Spring Full-Stack Practical Cases
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.