Maven插件Jib配合Harbor生成Docker镜像

1 说明

  1. Maven插件Jib暂不支持https的自签名,因此只能配置以Http的方式访问Harbor私有仓库
  2. 以下基于SpringBoot2.x进行配置

2 Maven配置

2.1 pom.xml中配置

项目的pom.xml中添加以下属性和插件内容:

<properties>
    <app.main.class>cc.anxminise.spblearn.Application</app.main.class>
    <docker.image.prefix>192.168.1.112/library</docker.image.prefix>
    <docker.image.name>${artifactId}:${version}</docker.image.name>
</properties>
属性 说明 举例
app.main.class SpringBoot应用的启动类,即:待@SpringBootApplication注解的类
docker.image.prefix 生成的docker镜像的tag标签的前缀 192.168.1.112/library/spblearn:0.0.1
docker.image.name 生成的docker镜像的名称 192.168.1.112/library/spblearn:0.0.1
<plugins>
    <plugin>
        <groupId>com.google.cloud.tools</groupId>
        <artifactId>jib-maven-plugin</artifactId>
        <version>0.9.11</version>
        <configuration>
          <from>
            <image>192.168.1.112/library/centos7-java8u131:1.0.1</image>
          </from>
          <to>
            <image>${docker.image.prefix}/${docker.image.name}</image>
          </to>
          <container>
            <jvmFlags>
              <jvmFlag>-Xms512m</jvmFlag>
            </jvmFlags>
            <environment>
              <spring.profiles.active>prod</spring.profiles.active>
              <TZ>Asia/Shanghai</TZ>
            </environment>
            <mainClass>${app.main.class}</mainClass>
            <format>OCI</format>
            <useCurrentTimestamp>true</useCurrentTimestamp>
          </container>
          <allowInsecureRegistries>true</allowInsecureRegistries>
        </configuration>
    </plugin>
</plugins>
属性 说明
jvmFlag 配置jvm虚拟机参数
spring.profiles.active 配置应用的启动参数,这里配置了使用application-prod.yml中的配置
TZ 配置了使用的时区

2.2 执行构建生成docker镜像

mvn jib:dockerBuild -DsendCredentialsOverHttp=true

3 本地测试镜像

docker run -it --rm -p port:port 192.168.1.112/library/镜像名称:镜像版本

4 推送镜像到仓库

docker push 192.168.1.112/library/镜像名称:镜像版本

猜你喜欢

转载自www.cnblogs.com/anxminise/p/9776056.html