Spring 與 Docker 筆記

基本操作概念與參考整理。

SpringBoot構建Docker鏡像的 3 種方式

  • 使用 spring-boot-maven-plugin 內置的 build-image.
    • mvn spring-boot:build-image
  • 使用 Google 的 jib-maven-plugin。
    • mvn compile jib:build (Build your container image) - 會推送
    • mvn compile jib:dockerBuild (Build to Docker daemon) - local
    • mvn compile jib:buildTar - local
  • 使用 dockerfle-maven-plugin。

一般docker打包流程

1
2
3
4
5
6
7
+---------+     +---------+           +-----------------------------------------------+
| Project |---->| JAR |-------+ | Docker Daemon |
+---------+ +---------+ | | +---------------+ build +-----------------+ | +------------------+
+---->| Build context |-------->| Container Image |------>| Container Image |
+------------+ | | +---------------+ | (docker cache) | | | (registry) |
| Dockerfile |----+ | +-----------------+ | +------------------+
+------------+ +-----------------------------------------------+

JIB 打包(唯一無須本機須安裝docker環境,就可打包成tarball)

1
2
3
4
+---------+       Jib      +------------------+
| Project |--------------->| Container Image |
+---------+ | (registry) |
+------------------+

參考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<plugins>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>${jib.version}</version>
<configuration>
<skip>${jib.skip}</skip>
<!-- 配置基礎image-->
<from>
<image>openjdk:8-jre-alpine</image>
</from>
<!-- 配置推送地址,倉庫名,image名稱-->
<to>
<image>${推送地址}/${倉庫名}/${image名稱}</image>
<tags>
<!-- <tag>${jib.repository.name}</tag>-->
<!-- <tag>${version}</tag>-->
</tags>
<auth>
<username>${帳號}</username>
<password>${密碼}</password>
</auth>
</to>
<!-- jib默認不推送到非https的私服-->
<!-- <allowInsecureRegistries>true</allowInsecureRegistries>-->
<container>
<mainClass>${jib.main.class}</mainClass>
<labels>
<name>${artifactId}</name>
</labels>
<ports>
<port>8080</port>
<port>8081</port>
</ports>
</container>
</configuration>
<!--綁定到maven lifecicle-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <phase>package</phase>-->
<!-- <goals>-->
<!-- <goal>build</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
</plugin>
</plugins>

Maven plugin 比較

Maven plugin 比較

註:

  • SpringBoot 原生的方式,什麼都不需要自己做,直接就能用。
  • 最有特點的是 Jib,不需要你本地安裝 Docker,可以直接推送到指定的倉庫。
  • 前2個與比較吃網路環境狀態

參考: Jib 官網