개발일기

spring boot에 docker 적용하기 본문

카테고리 없음

spring boot에 docker 적용하기

한둥둥 2023. 9. 24. 22:47

https://www.docker.com/get-started/

 

Get Started | Docker

Get started with Docker Desktop and join millions of developers in faster, more secure app development using containers and beyond.

www.docker.com

해당 사이트에서 자신의 컴퓨터에 맞는 OS를 설치해준다. 

Ubunut같은 경우 terminal창에서 명령어를 통해 설치해줌 >> 추후 다룰 예정 

 

docker 설치를 완료 후, 

docker --version을 통해서 docker가 정상적으로 설치되었는지 확인한다. 

 

이제 Spring boot project에 docker이미지로 빌드시, 생성하기 위해 Dockerfile을 생성하겠습니다. 

 

https://spring.io/guides/topicals/spring-boot-docker/

 

Getting Started | Spring Boot Docker

The Spring Boot Maven and Gradle plugins use buildpacks in exactly the same way that the pack CLI does in the following examples. The resulting images are identical, given the same inputs. Cloud Foundry has used containers internally for many years now, an

spring.io

해당 공식문서에 들어가 확인하면 DockerFile은 프로젝트의 최상단에 올리라는 것을 알 수 있습니다. 

 

프로젝트 최상단에 DockerFile을 생성. 

 

Dockerfile을 설정해주기전 ./gradlew build를 통해 빌드를 해준 후, 진행하시면 될 것 같습니다. 

혹시, 초창기 spring boot에 test코드 부분에 비어있는 상태 후 , 빌드를 해주신다면 에러가 발생하기 때문에 해당 부분은 주석처리해주시면 될 것 같습니다. 

package com.realworld.project;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ProjectApplicationTests {

	/*@Test
	void contextLoads() {
	}*/

}

 

Dockerfile은 도커의 설정을 이해주는 파일이라고 생각하시면 될 것 같습니다. 

현재 자바로 진행할 것이기 때문에 어떤 자바 버전을 사용할 것인지 설정 해주시면 되겠습니다. 

 

 

 

FROM eclipse-temurin:17
LABEL maintainer="hanseu9839@gmail.com"
ARG JAR_FILE=build/libs/project-0.0.1-SNAPSHOT.jar
ADD ${JAR_FILE} docker-realworld.jar
ENTRYPOINT ["java","-jar","docker-realworld.jar"]

Intelij에서 openjdk17을 사용하려고 보니, 해당 버전을 추가할 수 없어 확인한 결과 openjdk 17버전은 oracle에서 정식으로 지원되지 않는 것  같습니다. 

https://stackoverflow.com/questions/75181671/why-can-i-not-download-openjdk-17-in-my-intellij

 

Why can I not download OpenJDK-17 in my IntelliJ?

I have a Spring Boot application and I'm trying to set the JDK to OpenJDK-17. I've downloaded it from Java, then I go to IDE and Project Settings > Project Structure > SDKs, and then I click ...

stackoverflow.com

jdk17버전을 사용해야하기 때문에 adoptopenjdk가 아닌 eclipse-temurin:17을 사용하여 자바 버전을 세팅해주었습니다. 

LABEL부분에는 개인 이메일을 입력하고, ARG 에는 jar파일이 위치한 경로를 입력해주시면 될 것같습니다. 

 

ENTRYPOINT ["java","-jar","-Dspring.profiles.active=prod","/app.jar"]

운영 및 개발에서 사용되는 환경 설정을 분리해서 사용할 경우, 위에 처럼 ENTRYPOINT를 설정할 수 있습니다.

 

Dockerfile 설정 관련하여 공부를 해봐야 할 것 같습니다 

여기까지 작성 후, 

 docker build -t docker-realworld .

terminal에 입력해준 결과 

정상적으로 빌드 되었습니다. 

 

docker images

명령어를 입력하여 아래와 같이 이미지를 얻은 것을 확인할 수 있습니다.