使用jib-maven-plugin实现java应用docker化

增加插件依赖

引用结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.0.2</version>
<configuration>
<to>
<image>your_image</image>
</to>
</configuration>
</plugin>
...
</plugins>
</build>
...
</project>

构建docker镜像

1
mvn compile jib:build

或者使用本地存在的docker环境命令

1
mvn compile jib:dockerBuild

绑定生命周期

例如绑定到package,增加executions

1
2
3
4
5
6
7
8
9
10
11
12
13
<plugin>
<groupId>com.google.com.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
...
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>

就可使用
1
mvn package

使用案例

Java Main Class方式应用

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
48
49
50
51
52
53
54
55
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.0.2</version>
<configuration>
<from>
<image>openjdk:alpine</image>
</from>
<to>
<image>your_image</image>
<tags>
<tag>latest</tag>
</tags>
</to>
<container>
<jvmFlags>
<jvmFlag>-Xms512m</jvmFlag>
<jvmFlag>-Xdebug</jvmFlag>
<jvmFlag>-Xmy:flag=jib-rules</jvmFlag>
</jvmFlags>
<mainClass>yourpackage.app.main</mainClass>
<args>
<arg>some</arg>
<arg>args</arg>
</args>
<ports>
<port>8080</port>
<port>2000-2003/udp</port>
</ports>
<labels>
<key1>value1</key1>
<key2>value2</key2>
</labels>
<format>OCI</format>
</container>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>dockerBuild</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
...
</project>

解释:
1.从docker-hub拉取openjdk:alpine镜像作为基础镜像
2.构建标识为your_image的镜像,tag列表为latest
3.容器启动时将通过java -Xms512m -Xdebug -Xmy:flag=jib-rules -cp app/libs/*:app/resources:app/classes yourpackage.app.main some args来运行应用
4.并且暴露出端口8080,udp端口段2000-2003
5.增加两个label
6.使用OCI格式
7.docker构建绑定到package生命周期

JavaWeb War方式应用

例如

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
48
49
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.0.2</version>
<configuration>
<from>
<image>tomcat:8.5-jre8-alpine</image>
</from>
<to>
<image>your_image</image>
<tags>
<tag>latest</tag>
</tags>
</to>
<container>
<args>
<arg>some</arg>
<arg>args</arg>
</args>
<appRoot>/usr/local/tomcat/webapps/ROOT</appRoot>
<ports>
<port>8080</port>
</ports>
<labels>
<key1>value1</key1>
<key2>value2</key2>
</labels>
<format>OCI</format>
</container>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>dockerBuild</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
...
</project>

同上面Java Main Class方式基本一样,只是将忽略mainClassjvmFlags,这里新增了appRoot来指定容器内war包的部署路径

运行构建好的镜像

命令示例,请勿直接使用

1
2
3
4
5
6
7
8
9
10
docker service create \
--name="your_app" \
--network traefiknet \
--container-label traefik.backend="your_app" \
--container-label traefik.frontend.entryPoints="http,https" \
--container-label traefik.frontend.rule="Host: your.domain.com" \
--container-label traefik.frontend.redirect.entryPoint="https" \
--container-label traefik.port='8080' \
--container-label traefik.protocol='http' \
--replicas 1 your_image:latest

1
2
3
4
docker service create \
--name="your_app" \
--publish="8080:8080" \
--replicas 1 your_image:latest
1
docker run -d --name="your_app" -p 8080:8080 --restart=always your_image:latest

结束