Operations 4 min read

How to Simplify Local Middleware Setup with Docker Compose and Spring Boot

This guide explains how to replace manual installation of MySQL, Redis, and Kafka with a Docker Compose configuration and Spring Boot integration, providing step‑by‑step instructions, code snippets, and tips for controlling container lifecycle during development.

Eric Tech Circle
Eric Tech Circle
Eric Tech Circle
How to Simplify Local Middleware Setup with Docker Compose and Spring Boot

Why Replace Manual Middleware Installation?

Local development often relies on databases (MySQL), message queues (Kafka), and caches (Redis). Installing these services directly on a workstation leads to environment conflicts, inconsistent configurations across projects, and repetitive setup whenever a new environment is needed.

Solution: Docker Compose + Spring Boot

Using Docker Compose creates a fully containerized middleware stack that can be started and stopped with a single command, eliminating the need for local installations and ensuring consistent configurations.

01 Create docker-compose.yml

version: '3.9'
services:
  mysql:
    image: mysql:8.0
    container_name: mysql
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: demo
    ports:
      - "3306:3306"
    volumes:
      - mysql_data:/var/lib/mysql

  redis:
    image: redis:7.0
    container_name: redis
    ports:
      - "6379:6379"

  kafka:
    image: apache/kafka:latest
    container_name: kafka
    ports:
      - "9092:9092"

volumes:
  mysql_data:

02 Configure the Spring Boot Project

Add the Docker Compose starter dependency in build.gradle:

implementation "org.springframework.boot:spring-boot-docker-compose"

Then edit application-local.yml to point to the containers:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC
    username: root
    password: password

  redis:
    host: localhost
    port: 6379

  kafka:
    consumer:
      bootstrap-servers: localhost:9092
      auto-offset-reset: earliest
    producer:
      bootstrap-servers: localhost:9092
      value-serializer: org.springframework.kafka.support.serializer.JsonSerializer

03 Start the Application

Run the Spring Boot app with ./gradlew bootRun. Use docker ps to verify that MySQL, Redis, and Kafka containers are running. When the application stops, the containers are automatically removed.

04 Control Compose Activation

Enable or disable Docker Compose per profile by setting spring.docker.compose.enabled in the appropriate YAML file. For example, keep it false in application.yml and set it to true in application-local.yml:

spring:
  docker:
    compose:
      enabled: false

Conclusion

Integrating Spring Boot with Docker Compose provides a seamless, reproducible local middleware environment, reduces manual configuration effort, and improves development efficiency.

Appendix

Docker Desktop view after starting the Spring Boot project:

Docker containers after start
Docker containers after start

Docker Desktop view after stopping the application (containers are removed):

Docker containers after stop
Docker containers after stop
DockerMiddlewareDevOpsSpring BootcontainersDocker ComposeLocal Development
Eric Tech Circle
Written by

Eric Tech Circle

Backend team lead & architect with 10+ years experience, full‑stack engineer, sharing insights and solo development practice.

0 followers
Reader feedback

How this landed with the community

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.