본문 바로가기
Back-end/Spring Boot

SpringBoot 3.1의 Docker Compose

by whatamigonnabe 2023. 8. 1.

MSA처럼 여러 어플리케이션을 구동하고 각각이 독립적인 DB를 가지게 되는 상황에서, 각 DB Server의 port 를 관리하는 것이 어렵기 때문에 중복 문제가 발생할 수도 있습니다.

 

SpringBoot 3.1에서는 이러한 문제를 해결해줍니다. 각각 DB의 port를 명시하지 않고 random port를 사용하고 properties에 명시할 필요도 없어졌고, username, password 등등 또한 명시할 필요가 없어졌습니다.

 

SpringBoot 3.1에서는 서비스를 구동하기 전에 docker compose up 를 먼저 구동해서 서비스들을 연결해줍니다. 이미 구동중이라면 그것을 감지해서 그것을 사용합니다. 서비스를 멈추면 자동으로 docker compose stop을 실행합니다.

Docker Compose에서 사용하는 아미지를 자동으로 감지해서 ConnectionDetails 빈을 생성하고 해당 서비스를 가리킵니다. 이를 통해 이미지와 연결에 필요한 것들을 개발자가 진행할 필요가 없고, 개발자는 compose.yaml만 작성하면 됩니다.

 

Spring이 지원하는 이미지는 다음과 같습니다.

start.spring.io에서 Docker Compose support를 선택하고 사용하는 DB를 선택하면 자동으로 구동이 된다.

커스텀 이미지도 지원이 가능합니다.

무시하기

compose.yaml 에 아래처럼 작성하면 컨테이너를 구동하고 연결하지 않습니다.

services:
  redis:
    image: 'redis:7.0'
    ports:
      - '6379'
    labels:
      org.springframework.boot.ignore: true

 

사용하기

아래처럼 Docker Compose Support와 MySQL Driver 의존성 설정을 해줍니다.

 

이후 살펴보면, 자동으로 compose.yaml 파일이 생성된 것을 볼 수 있고, 외부 포트 번호는 random으로 사용하고 있습니다.

구동해보니, 먼저 컨테이너를 실행시킨 후에 어플리케이션을 실행시켜 연동합니다.

 

클라우드를 사용하면 정말 편한 기능인 것을 느낄 수 있었습니다. 자세한 설명은 아래 링크에서 확인할 수 있습니다.

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features.docker-compose

참조

 

Docker Compose Support in Spring Boot 3.1

Docker Compose Support in Spring Boot 3.1 Docker Compose support in Spring Boot 3.1 builds on top of the ConnectionDetails abstraction, which we've featured in a separate blog post. If you haven't already read it, please do so before reading this post. Doc

spring.io

 

 

Spring Boot Reference Documentation

This section goes into more detail about how you should use Spring Boot. It covers topics such as build systems, auto-configuration, and how to run your applications. We also cover some Spring Boot best practices. Although there is nothing particularly spe

docs.spring.io