介紹
您已經建立了一個 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 映像:
-
為 Spring Boot 專案建立 jar 檔案:
./gradlew bootJar # For Gradle build system
或
./mvnw spring-boot:build-jar # For Maven build system
-
使用 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} .
-
驗證 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) } } </bootjar>
<!-- 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}") } </bootbuildimage>
// 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中文網其他相關文章!

本文分析了2025年的前四個JavaScript框架(React,Angular,Vue,Susve),比較了它們的性能,可伸縮性和未來前景。 儘管由於強大的社區和生態系統,所有這些都保持占主導地位,但它們的相對人口

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

Node.js 20通過V8發動機改進可顯著提高性能,特別是更快的垃圾收集和I/O。 新功能包括更好的WebSembly支持和精製的調試工具,提高開發人員的生產率和應用速度。

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA

本文介紹了SnakeyAml中的CVE-2022-1471漏洞,這是一個允許遠程代碼執行的關鍵缺陷。 它詳細介紹瞭如何升級春季啟動應用程序到Snakeyaml 1.33或更高版本的降低風險,強調了依賴性更新

本文使用lambda表達式,流API,方法參考和可選探索將功能編程集成到Java中。 它突出顯示了通過簡潔性和不變性改善代碼可讀性和可維護性等好處

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Atom編輯器mac版下載
最受歡迎的的開源編輯器