1. 程式人生 > 實用技巧 >「Spring Boot 2.4 新特性」一鍵構建Docker映象

「Spring Boot 2.4 新特性」一鍵構建Docker映象

背景

在我們開發過程中為了支援 Docker 容器化,一般使用 Maven 編譯打包然後生成映象,能夠大大提供上線效率,同時能夠快速動態擴容,快速回滾,著實很方便。docker-maven-plugin 外掛就是為了幫助我們在 Maven 工程中,通過簡單的配置,自動生成映象並推送到倉庫中。

spotify 、fabric8

  • 這裡主要使用的主要是如下兩種外掛 spotifyfabric8 , ... -配置通過 xml 定義出 Dockerfile 或者掛載外部 Dockerfile 通過呼叫 Docker remote api 構建出映象

  • pig 微服務平臺所有的容器化都是基於此構建


<plugin>
  <groupId>com.spotify</groupId>
  <artifactId>docker-maven-plugin</artifactId>
  ...  -配置通過 xml 定義出 Dockerfile 或者掛載外部 Dockerfile
</plugin>


<plugin>
  <groupId>io.fabric8</groupId>
  <artifactId>docker-maven-plugin</artifactId>
   ...  -配置通過 xml 定義出 Dockerfile 或者掛載外部 Dockerfile
</plugin>

  • 執行相應的外掛週期即可 mvn docker:build && mvn docker:push

jib

  • 專案每次釋出實際上變更的程式碼量不大,尤其依賴的 jar 變動的可能性較小,如果使用前兩種外掛構建映象,會導致每次都全量構建,會導致儲存和頻寬資源浪費。

  • jib 是 Google 於 18 年 7 月釋出的一個針對 Java 應用的構建映象的工具(支援 Maven 和 Gradle) ,好處是能夠複用構建快取,能夠加快構建,減小傳輸體積

<!--配置通過 xml 定義出 Dockerfile ,本質上和外掛 Dockerfile 並無區別-->
<plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>jib-maven-plugin</artifactId>
</plugin>

mvn jib:dockerBuild

以上三種方案的問題

  • 在實際開發過程中,大部分的 spring boot 專案構建 Dockerfile 都是相同,不需要通過的 XML 或者通過外掛 Dockerfile 來重新定義

  • 以上外掛需要對 Dockerfile 的定義知識有相對的了 對開發並不友好

  • 沒充分理由 Spring Boot 2.3 以後的 Jar 分層技術。

解決方案

  • Spring Boot 2.4 推出了自己的 docker 構建工具 整合在原有的 spring-boot-maven-plugin 中,只需要配置對應目標倉庫和主機資訊即可完成映象構建。

  • 如下配置即可完成上圖中 通過開發機器在不安裝 Docker 的同時,通過 192.168.0.10 的 Docker Remote API 完成映象構建併發布到 192.168.0.20 的映象倉庫
 <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <image>
            <name>192.168.0.20/pig4cloud/${project.artifactId}</name>
            <!-- 執行完build 自動push -->
            <publish>true</publish>
        </image>
        <!--配置構建宿主機資訊,本機不用配置-->
        <docker>
			<host>http://192.168.0.10:2375</host>
            <tlsVerify>false</tlsVerify>
            <publishRegistry>
                <username>username</username>
                <password>password</password>
                <url>192.168.0.20</url>
            </publishRegistry>
        </docker>
    </configuration>
</plugin>
  • 執行以下命令即可完成 映象的構建和自動釋出
mvn spring-boot:build-image

其他說明

docker host 配置不生效

  • 如下圖 ① 處配置 節點,但是 ② 報錯提示 host 不一致

  • 檢查本地是否配置 $DOCKER_HOST 環境變數,經過閱讀原始碼後發現優先讀取此變數。
⋊> ~ echo $DOCKER_HOST                                                  11:07:51
tcp://172.17.0.111:2375

網路支援

  • 擷取部分構建過程中的日誌,如下需要從 github 下載相關的依賴 約 100M ,這個過程大概率會失敗。建議通過配置代理或者使用國外 ECS 來解決。
 :: Spring Boot ::                (v2.4.0)
[INFO]  > Running creator
[INFO]     [creator]         Downloading from https://github.com/bell-sw/Liberica/releases/download/8u275+1/bellsoft-jre8u275+1-linux-amd64.tar.gz
[INFO]     [creator]       JVMKill Agent 1.16.0: Contributing to layer
[INFO]     [creator]         Downloading from https://github.com/cloudfoundry/jvmkill/releases/download/v1.16.0.RELEASE/jvmkill-1.16.0-RELEASE.so
[INFO]     [creator]         Downloading from https://repo.spring.io/release/org/springframework/cloud/spring-cloud-bindings/1.6.0/spring-cloud-bindings-1.6.0.jar
[INFO]     [creator]         Verifying checksum
[INFO]     [creator]           192.168.0.20/pig4cloud/demo:latest
[INFO]
[INFO] Successfully built image '192.168.0.20/pig4cloud/demo:latest'
[INFO]  > Pushing image '192.168.0.20/pig4cloud/demo:latest' 100%
[INFO]  > Pushed image '192.168.0.20/pig4cloud/demo:latest'
[INFO] BUILD SUCCESS

專案推薦: Spring Cloud 、Spring Security OAuth2的RBAC許可權管理系統 歡迎關注