首頁 > Java > java教程 > 主體

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

WBOY
發布: 2024-09-08 20:31:02
原創
863 人瀏覽過

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.named<BootJar>("bootJar") {
   layered {
      enabled.set(false)
   }
}
登入後複製
<!-- pom.xml -->
<project>
   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
               <layers>
                  <enabled>true</enabled>
               </layers>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>
登入後複製

您甚至可以調整圖層的建立方式。請參閱 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.named<BootBuildImage>("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 使用其他鏡像名稱,如下所示:

<properties>
   <imagePrefix>javarush</imagePrefix>
</properties>

...

<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <image>
                        <name>${imagePrefix}/${project.artifactId}:${project.version}</name>
                    </image>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
登入後複製

執行命令時配置鏡像名稱

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

./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中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!