Boot

Docker Compose Support in Spring Boot

In today’s rapidly evolving software landscape, the efficient deployment and management of applications are paramount. This article explores the pivotal role of Docker Compose support in Spring Boot 3 and why it is indispensable for modern Spring Boot applications.

1. Introduction

As applications become increasingly complex and microservices-oriented, orchestrating multiple containers efficiently is a challenging task. Docker Compose offers a robust solution, allowing developers to define, configure, and run multi-container applications seamlessly.

For Spring Boot applications, Docker Compose support provides a compelling advantage. It simplifies the deployment process, promotes scalability, and ensures consistency across development, testing, and production environments. By embracing Docker Compose, Spring Boot applications can thrive in today’s dynamic and competitive software ecosystem.

2. Spring Boot 3 Docker Compose Features

Before Spring Boot 3.1, integrating such Docker Compose services into your application required manual configuration. Developers had to specify host ports, usernames, passwords, and other details in their application properties. This approach posed challenges, especially when dynamic ports changed upon container restarts.

With Spring Boot 3.1, handling Docker Compose services becomes remarkably straightforward. You can continue using dynamic host ports without the hassle of configuring properties or worrying about port conflicts. Here’s how Spring Boot 3.1 simplifies the process:

FeatureDescription
Automatic Docker Compose ManagementSpring Boot 3.1 detects the presence of a Docker Compose file and handles the execution of docker compose up automatically. It ensures that required services are running or starts them when necessary.
Seamless ConnectionThe magic happens behind the scenes. Spring Boot 3.1 seamlessly connects to Docker Compose services, eliminating the need for you to remember port numbers, usernames, or passwords.
Graceful ShutdownNo more lingering Docker containers consuming memory. Spring Boot 3.1 takes care of shutting down Docker Compose services with docker compose stop when your application gracefully exits.

Effortless Development and Deployment

Thanks to Spring Boot 3.1’s Docker Compose support, all you need is the compose.yaml file. Spring Boot figures out the rest, making development and deployment almost effortless. It’s a leap forward in simplifying microservices development and ensuring that your applications work seamlessly within a containerized environment.

Supported Docker Images

As of now, Spring Boot 3.1 supports a variety of Docker images, including Cassandra, Elasticsearch, Oracle Database, MariaDB, Microsoft SQL Server, MySQL, PostgreSQL, MongoDB, RabbitMQ, Redis, and Zipkin. This extensive support caters to a wide range of application needs.

3. Create a Spring Boot Project

You can use the Spring Initializer or your preferred method to create a Spring Boot project. Make sure to include the necessary dependencies for web, actuator, and Docker Compose support in your project.

Fig.1: Spring boot Initializer.
Fig.1: Spring boot Initializer.

3.1 Spring Boot pom.xml

Here is our pom.xml file of our test run:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.1.3</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>Docker-Compose-Support-in-Spring-Boot-Applications</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Docker-Compose-Support-in-Spring-Boot-Applications</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-docker-compose</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

3.2 Create a Docker Compose File

We create a file named compose.yml to define our services, and we save it in the project’s source folder. In this example, we will setup two different services:

  • postgres
  • redis

This is our file:

version: '3.1'

services:
  db:
    image: postgres
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: p@ssw0rd
      POSTGRES_DB: book
    ports:
      - "5432:5432"
    volumes:
      - postgres_dev:/var/lib/postgresql/data

  redis:
    image: "redis/redis-stack:latest"
    ports:
      - "6379:6379"
      - "8001:8001"
    environment:
      - REDIS_REPLICATION_MODE=master

volumes:
  postgres_dev:

4. Run the Spring Boot Application

You can run the Spring Boot application using your IDE.

Fig. 2: Run the Spring Boot Application
Fig. 2: Run the Spring Boot Application

By running this command

docker ps -a

We can see that two containers were created and running as long as our spring boot application is still running.

Fig. 3: docker compose containers
Fig. 3: docker compose containers

When we kill the application, the two containers are also terminated!

Fig. 4: docker compose containers exited
Fig. 4: docker compose containers exited

5. Testing Docker Compose Support

You can test the application by making HTTP requests to the defined endpoints.

For example, we can go to this url:

http://localhost:8080/actuator/health

And see that our Spring Boot Application is indeed running.

Fig. 5: Spring Boot App
Fig. 5: Spring Boot App

To check that our containers are accessible, we can go to our data structure store, Redis, using this url:

http://localhost:8001/
Fig. 6: Redis data structure store - Docker Compose Support
Fig. 6: Redis data structure store – Docker Compose Support

6. Download the Source Code

This was an example of how to integrate Docker Compose into our Spring Boot Application.

Download
You can download the full source code of this example here: Docker Compose Support in Spring Boot Applications

Odysseas Mourtzoukos

Mourtzoukos Odysseas is studying to become a software engineer, at Harokopio University of Athens. Along with his studies, he is getting involved with different projects on gaming development and web applications. He is looking forward to sharing his knowledge and experience with the world.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button