網頁

2020/12/13

Spring Boot Jib Maven build image to Docker Hub

使用GoogleContainerTools下的Jib maven plugin build Spring Boot docker image並推送到Docker Hub registry。

在「String Boot Jib Maven build docker image」僅用Jib把local的Spring Boot專案建置成local Docker daemon的image;本篇則是用Jib將Spring Boot專案建置為image後推送到Docker Hub registry。

範例環境:

  • Java 8
  • Spring Boot 2.3.2.RELEASE
  • Maven 3.6.3
  • Docker 19.03.12

註冊一個Docker Hub帳戶並記下帳戶名稱(DOCKER_ID)。

建立Spring Boot專案(參考IntelliJEclipse)。

在專案的pom.xml<plugins> ... </plugins>中加入Jib maven plugin設定。<image>中的docker_id為Docker Hub帳號。<auth>中為Docker Hub帳號密碼。

<plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>jib-maven-plugin</artifactId>
    <version>2.7.0</version>
    <configuration>
        <to>
            <image>docker.io/docker_id/spring-boot-demo</image>
            <auth>
                <username>docker_id</username>
                <password>********</password>
            </auth>
        </to>
    </configuration>
</plugin>

參考pom.xml


將工作目錄移到Spring Boot專案的根目錄下,輸入mvn compile jib:build開始建置image並推送到Docker Hub registry。

~/../spring-boot-demo$ mvn compile jib:build
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.abc:spring-boot-demo >----------------------
[INFO] Building spring-boot-demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ spring-boot-demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ spring-boot-demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- jib-maven-plugin:2.7.0:build (default-cli) @ spring-boot-demo ---
[WARNING] 'mainClass' configured in 'maven-jar-plugin' is not a valid Java class: ${start-class}
[INFO]
[INFO] Containerizing application to docker_id/spring-boot-demo...
[WARNING] Base image 'gcr.io/distroless/java:8' does not use a specific image digest - build may not be reproducible
[INFO] Using credentials from <to><auth> for docker_id/spring-boot-demo
[INFO] Using base image with digest: sha256:90596f62a8ec0701d00845b85dfccf7f53b5e0bb3c49e275d7f19ea6c3e4565d]
[INFO]
[INFO] Container entrypoint set to [java, -cp, /app/resources:/app/classes:/app/libs/*, com.abc.demo.DemoApplication]
[INFO]
[INFO] Built and pushed image as docker_id/spring-boot-demo
[INFO] Executing tasks:
[INFO] [============================  ] 91.7% complete
[INFO] > launching layer pushers
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  8.091 s
[INFO] Finished at: 2020-12-13T23:28:16+08:00
[INFO] ------------------------------------------------------------------------

完成後便能在Docker Hub的Repositories看到推送的image。


沒有留言:

張貼留言