본문 바로가기

원도우

[원도우] Spring Boot 프로젝트 Gradle로 Docker 이미지 빌드하기

반응형

Spring Boot 프로젝트 Gradle로 Docker 이미지 빌드하기

테스트 환경

>systeminfo | findstr "OS 이름"
OS 이름:                 Microsoft Windows 11 Pro
>gradlew --version

------------------------------------------------------------
Gradle 7.6
------------------------------------------------------------

Build time:   2022-11-25 13:35:10 UTC
Revision:     daece9dbc5b79370cc8e4fd6fe4b2cd400e150a8

Kotlin:       1.7.10
Groovy:       3.0.13
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          17.0.5 (Oracle Corporation 17.0.5+9-LTS-191)
OS:           Windows 11 10.0 amd64

수동으로 애플리케이션 빌드 및 도커 이미지 빌드

애플리케이션 빌드

gradlew build

Dockerfile 생성

Dockerfile
FROM openjdk:17.0.2-jdk

ENV APP_HOME=/apps

ARG JAR_FILE_PATH=build/libs/demo-0.0.1-SNAPSHOT.jar

WORKDIR $APP_HOME

COPY $JAR_FILE_PATH app.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "app.jar"]

도커 이미지 빌드

docker build --tag anti1346/dockerforspringboot:0.0.1 .

빌드된 도커 이미지 확인

docker images --filter=reference="anti1346/dockerforspringboot:0.0.1"

도커 컨테이너 실행

docker run -d -p 8080:8080 anti1346/dockerforspringboot:0.0.1

도커 멀티스테이지(Multi-Stage) 빌드

Dockerfile 생성

Dockerfile
FROM gradle:7.6-jdk17-alpine as build

ENV APP_HOME=/apps

WORKDIR $APP_HOME

COPY build.gradle settings.gradle gradlew $APP_HOME

COPY gradle $APP_HOME/gradle

RUN chmod +x gradlew

RUN ./gradlew build || return 0

COPY src $APP_HOME/src

RUN ./gradlew clean build

FROM openjdk:17.0.2-jdk

ENV APP_HOME=/apps
ARG ARTIFACT_NAME=app.jar
ARG JAR_FILE_PATH=build/libs/demo-0.0.1-SNAPSHOT.jar

WORKDIR $APP_HOME

#COPY --from=build /apps/build/libs/demo-0.0.1-SNAPSHOT.jar app.jar
COPY --from=build $APP_HOME/$JAR_FILE_PATH $ARTIFACT_NAME

EXPOSE 8080

#ENTRYPOINT ["java", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-Djava.security.egd=file:/dev/./urandom", "-jar", "app.jar"]
ENTRYPOINT ["java", "-jar", "app.jar"]

도커 이미지 빌드

docker build --tag anti1346/multistagebuildforspringboot:0.0.1 .

빌드된 도커 이미지 확인

docker images --filter=reference="anti1346/multistagebuildforspringboot:0.0.1"

도커 컨테이너 실행

docker run -d -p 8080:8080 anti1346/multistagebuildforspringboot:0.0.1

 

>curl localhost:8080
Hello World! ==> Multi-Stage Build
728x90

gradle(Spring Boot Gradle Plugins)로 도커 이미지 빌드

"com.palantir.docker" 플러그인 추가

 

build.gradle

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.0.1'
    id 'io.spring.dependency-management' version '1.1.0'
    id "com.palantir.docker" version "0.34.0"
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

도커 이미지 빌드

gradlew clean bootBuildImage
gradlew clean bootBuildImage --imageName=anti1346/bootbuildimageforspringboot:0.0.1

빌드된 도커 이미지 확인

docker images --filter=reference="anti1346/bootbuildimageforspringboot:0.0.1"

도커 컨테이너 실행

docker run -d -p 8080:8080 anti1346/bootbuildimageforspringboot:0.0.1

 

>curl localhost:8080
Hello World! ==> Boot Build Image

 

참고URL

- Spring Boot Docker : https://spring.io/guides/topicals/spring-boot-docker/

- Dockerfile reference : https://docs.docker.com/engine/reference/builder/

- com.palantir.docker : https://plugins.gradle.org/plugin/com.palantir.docker

- gradle : https://hub.docker.com/_/gradle/tags?page=1&name=jdk17-al

 

728x90
반응형