首頁  >  文章  >  Java  >  使用 Buildpack 建立 Spring Boot 應用程式的 Docker 映像

使用 Buildpack 建立 Spring Boot 應用程式的 Docker 映像

WBOY
WBOY原創
2024-09-08 20:31:02870瀏覽

Creating Docker Image of Spring Boot Application using Buildpacks

介紹

您已經建立了一個 Spring Boot 應用程式。它在您的本地電腦上運行良好,現在您需要將該應用程式部署到其他地方。在某些平台上,您可以直接提交jar文件,它將被部署。在某些地方,您可以啟動虛擬機,下載原始程式碼,建置並運行它。但是,大多數時候您需要使用容器來部署應用程式。大多數時候,Docker 用於在容器中建置和運行映像。此外,當您將 jar 檔案上傳到某些平台時,應用程式會在背景的容器內執行。

因此,在本部落格中,我們將看到 3 種不同的方法來為給定的 Spring Boot 應用程式建立 Docker 映像。讓我們開始吧:

基礎容器鏡像

為任何應用程式建立 Docker 映像的簡單且不充分的方法是使用一個簡單的 Dockerfile,該檔案將 jar 檔案複製到映像中並使用 java -jar 命令運行它。

建立 Dockerfile

這是 Dockerfile,您可以將其放在專案的根目錄中:

FROM eclipse-temurin:21-jre-ubi9-minimal

ARG JAR_FILE

COPY ${JAR_FILE} application.jar

ENTRYPOINT ["java", "-jar", "/application.jar"]

我們指定了一個參數 JAR_FILE,它是要使用的 jar 檔案的位置。

建置 Docker 映像

建立上述 Dockerfile 後,使用下列步驟建立 Docker 映像:

  1. 為 Spring Boot 專案建立 jar 檔案:

    ./gradlew bootJar # For Gradle build system
    


    ./mvnw spring-boot:build-jar # For Maven build system
    
  2. 使用 Dockerfile 使用最新的 jar 檔案建構 Docker 映像。在以下命令中,將 {IMAGE_NAME} 替換為所需的映像名稱,將 {JAR_FILE} 替換為產生的 jar 檔案的路徑。圖像名稱也包含一個標籤,例如 - mycompany/product-service:0.0.1-SNAPSHOT:

    docker build --build-arg JAR_FILE={JAR_FILE} --tag {IMAGE_NAME} .
    
  3. 驗證 Docker 映像是否是使用以下命令建置的。您應該可以看到上面命令中指定名稱的圖像:

    docker images
    

使用分層 Jar 的高效容器鏡像

雖然可以輕鬆地將 Spring Boot uber jar 打包為 Docker 映像(如前面的方法所述),但在 Docker 映像中按原樣複製和運行 fat jar 有許多缺點。例如,

  • 在不解壓縮的情況下運行 uber jar 會產生一些額外的開銷。
  • 將應用程式的程式碼及其所有依賴項放在單一層中並不是最佳選擇。

由於我們編譯程式碼的頻率比升級 Spring Boot 版本的頻率高,因此最好將各個部分分開一些。如果我們將那些很少更改的jar檔案放在應用層之前的層中,那麼Docker通常只需要更改底層,並且可以從其快取中選取其餘的。

啟用分層 Jar

要建立分層 Docker 映像,我們需要先建立分層 jar。如今,它在 Gradle 和 Maven 中預設啟用。您可以使用以下設定啟用或停用分層 jar 行為:

// build.gradle
tasks.named("bootJar") {
    layered {
        enabled = false
    }
}
// build.gradle.kts
tasks.named7e534d3bd056dd41a31367722242bb81("bootJar") {
   layered {
      enabled.set(false)
   }
}
7a7963d93b467c9ab002af7a44e64bd4
c70346ba2d7be186a93fef826469bd2f
   5ac54a929980b32fdd2e48b6a8da067c
      c4222ff3007372f15ec728b963e717b6
         33ca7039399a4759a8764ea38765dd46
            05a8acc5c31084a4f61ade01873802caorg.springframework.boot192ca2f7b8c770b01c8f81e6bdd5b947
            9bc4cbb67f66e148869423c0d27e5f90spring-boot-maven-pluginb68fb17cb904a46b73e6272850323873
            f9d9f4a8f32d97f3ef0c10742ed31240
               e72a6d14eb9b15c1c12186917135155b
                  d79c9ed277a80783d30ca709c63bc2d1true1d1b3b9680fd03597e1ed3fa2b627c20
               98a78221d1c969529e0b9f39a24b39b0
            4b1b9d85fe86862ae3eab7e2045cf8a0
         9424ec9162a9b85f34067d0c54f34b32
      f3567a435ac6132b26525998d793443b
   f82c24fb6fac67972f6e79c18ccef56a
1755c7176d4002e2a322de4f48c796fa

您甚至可以調整圖層的建立方式。請參閱 gradle 或 maven 配置的文件。

建立 Dockerfile

以下是 Dockerfile,可用於利用分層 jar 並建立 Spring Boot 應用程式的分層 Docker 映像。

# Perform the extraction in a separate builder container
FROM eclipse-temurin:21-jre-ubi9-minimal AS builder

WORKDIR /builder

# This points to the built jar file in the target folder
# Adjust this to 'build/libs/*.jar' if you're using Gradle
ARG JAR_FILE=target/*.jar

# Copy the jar file to the working directory and rename it to application.jar
COPY ${JAR_FILE} application.jar

# Extract the jar file using an efficient layout
RUN java -Djarmode=tools -jar application.jar extract --layers --destination extracted

# This is the runtime container
FROM eclipse-temurin:21-jre-ubi9-minimal

WORKDIR /application

# Copy the extracted jar contents from the builder container into the working directory in the runtime container
# Every copy step creates a new docker layer
# This allows docker to only pull the changes it really needs
COPY --from=builder /builder/extracted/dependencies/ ./
COPY --from=builder /builder/extracted/spring-boot-loader/ ./
COPY --from=builder /builder/extracted/snapshot-dependencies/ ./
COPY --from=builder /builder/extracted/application/ ./

# Start the application jar - this is not the uber jar used by the builder
# This jar only contains application code and references to the extracted jar files
# This layout is efficient to start up and CDS friendly
ENTRYPOINT ["java", "-jar", "application.jar"]

建置 Docker 映像

建置分層 Docker 映像的步驟與建置基本 Docker 映像相同。請參考那裡。

雲端原生建置包

如果我告訴你不需要建立 Dockerfile 就可以建立 Docker 映像,你會怎麼樣?我們可以使用 Cloud Native Buildpacks 直接從 Gralde 或 Maven 外掛程式建置 docker 映像。一些平台(如 Heroku 或 Cloud Foundry)使用 Buildpack 將提供的 jar 檔案轉換為可運作的映像。

Spring Boot 包括直接對 Maven 和 Gradle 的建置包支援。我們不需要包含任何額外的插件。只需執行以下命令:

./gradlew bootBuildImage # For gradle build system


./mvnw spring-boot:build-image # For maven build system

上述指令產生預設名稱為 {PROJECT_NAME}:${PROJECT_VERSION} 的圖片。如果你想設定產生圖片的名稱,可以依照下列步驟操作:

為 Gradle 建置系統配置鏡像名稱

我們可以設定 bootBuildImage 任務來設定鏡像的名稱,如下所示:

// For build.gradle.kts
val imagePrefix = "javarush"
val dockerImageName = "docker-example"
tasks.named2e00f062267875fb651ebdd74d6c6eb1("bootBuildImage") {
   imageName.set("${imagePrefix}/${dockerImageName}:${version}")
}
// For build.gradle
def imagePrefix = "javarush"
def dockerImageName = "docker-example"
tasks.named("bootBuildImage") {
   imageName = "${imagePrefix}/${dockerImageName}:${version}"
}

為 Maven 建置系統配置鏡像名稱

我們可以設定 spring-boot-maven-plugin 使用其他鏡像名稱,如下所示:

a01741acb88936e4cea4237317380dd9
   d025b153d4d19d25710ee358f3c66eafjavarush515db9ab99a6918e33f7eeaddd64c8cf
86cd8b5c0fbd401fb69261955cf13796

...

c70346ba2d7be186a93fef826469bd2f
    5ac54a929980b32fdd2e48b6a8da067c
        c4222ff3007372f15ec728b963e717b6
            33ca7039399a4759a8764ea38765dd46
                05a8acc5c31084a4f61ade01873802caorg.springframework.boot192ca2f7b8c770b01c8f81e6bdd5b947
                9bc4cbb67f66e148869423c0d27e5f90spring-boot-maven-pluginb68fb17cb904a46b73e6272850323873
                f9d9f4a8f32d97f3ef0c10742ed31240
                    dc0870658837139040642baa5555a380
                        8a11bc632ea32a57b3e3693c7987c420${imagePrefix}/${project.artifactId}:${project.version}df406f776eecbaf16b62325323196f14
                    f8e950ebc6c1ea27f76cf997b9216fe6
                4b1b9d85fe86862ae3eab7e2045cf8a0
            9424ec9162a9b85f34067d0c54f34b32
        f3567a435ac6132b26525998d793443b
    f82c24fb6fac67972f6e79c18ccef56a
1755c7176d4002e2a322de4f48c796fa

執行命令時配置鏡像名稱

我們甚至可以在執行建置鏡像的指令時定義鏡像的名稱。

./gradlew bootBuildImage --imageName=javarush/docker-example:1.0.0 # For grade build system

./mvnw spring-boot:build-image -Dspring-boot.build-image.imageName=javarush/docker-example:1.0.0 # For maven build system

You can see the documentation to further configure Gradle or Maven plugin.
This is my go-to method to create a Docker image for any Spring Boot application.

Running Docker Container

Once you create a docker image, you need to make sure that it works as expected. After you make sure that the image is created, you can directly run it using the docker run command. For example,

docker run -p "8080:8080" {IMAGE_NAME}

But, this is not how images are used in production applications. Docker Compose is used to run and manage multiple docker images.

Conclusion

In this blog, we have seen how to build Docker images for Spring Boot applications using different methods. Being able to build docker images for your apps is a must skill to know because the image is what gets delivered. Thanks for reading the article till the end. I appreciate it. I will meet you in the next one. As always, all feedback and suggestions are welcome.

以上是使用 Buildpack 建立 Spring Boot 應用程式的 Docker 映像的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn