Spring Boot 3.1 ⚔️ Docker Compose

Dimas Adriyanto Setyawan
4 min readJun 4, 2023

Hello everyone! Today I’m going to talk about Spring Boot 3.1, which was recently released. There are many great features in there, but I’d like to talk about one specific upgrade that I believe is a powerful change in this release version, so let’s dive in! 🤿

The Spring Boot docker-compose module now has the ability to start and stop your container automatically; that means Spring-boot will find the docker-compose file and start it when the application starts up!

The other update is that you don’t have to duplicate the properties that are in that Docker compose file over to something like application.properties, so this will simplify your development.

I really excited with this enhancement so let’s try to do a demo and see how it works. 🥷🏻

Firstly, we are going to create a demo project using Intellij IDEA, or you could also use Spring Initializer as you like.

Go to : file -> new -> project -> Spring Initializer

In this project, I will use Java 17 and the open-source build tool Gradle, as shown in the picture below.

In the second step, we are going to use Spring Boot version 3.1.0 with the dependencies Spring Web,Spring Data JPA, Lombok, and PostgreSQL Driver, as shown in the picture below:

This is what the project structure will look like: 🚣‍♀️

Now let’s create Order model

package com.dimascode.demo.model;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity(name = "orders")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Order {

@Id
private Integer id;

private String name;

private String description;

}

Create OrderRepository

package com.dimascode.demo.repository;

import com.dimascode.demo.model.Order;
import org.springframework.data.repository.ListCrudRepository;

public interface OrderRepository extends ListCrudRepository<Order, Integer> {
}

Create OrderController

package com.dimascode.demo.rest;

import com.dimascode.demo.model.Order;
import com.dimascode.demo.repository.OrderRepository;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@AllArgsConstructor
public class OrderController {

private OrderRepository repository;

@GetMapping(value = "/orders")
public List<Order> getOrders() {
return repository.findAll();
}
}

Last but not least, let’s create docker-compose.yml.

See also the following docker-compose file naming conventions that are supported here: Docker compose

version: '3.1'


services:
db:
image: postgres
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_USERNAME: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: merchant_db

Once everything is set up, let’s talk about the previous version of Spring Boot. In the previous version, I would have to go to resources — application.properties — and fill out a bunch of properties on how to connect to a data source. Thankfully, Spring Boot 3.1 has simplified this, so we don’t have to configure it in the application.properties anymore!

Now let’s run the Spring Boot application :

OH no! We could not connect to our database right now because we haven’t filled anything in application.properties, and we also need to run docker-compose.yaml separately.

But don’t worry! in this case, Spring Boot 3.1 is not working out of the box; rather, we have to add the dependency below:

Gradle

dependencies {
developmentOnly("org.springframework.boot:spring-boot-docker-compose")
}

Maven

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

Now let’s run the Spring Boot application again, and as shown in the picture below, the first spring boot application is looking for the docker-compose.yml file; once it’s found, spring boot will automatically manage containers.

The last step is to try to persist a single record to make sure our Postgres database is working perfectly.

package com.dimascode.demo;

import com.dimascode.demo.model.Order;
import com.dimascode.demo.repository.OrderRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringBootDcApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootDcApplication.class, args);
}

@Bean
CommandLineRunner runner(OrderRepository repository) {
return args -> repository.save(new Order(1, "potato", "I order a lot of potato"));
}

}

Now run the spring boot application and get data with our get API, as shown below.

The full project can be found on my GitHub: dimasadriyantos

So that’s all, peeps! Shoutout to the Spring Boot team for such a great developer experience! And if you found this article useful, kindly share and give a clap for better reach, and then I’ll see you in the next one! CIAO ✋🏼

--

--