Docker使用Jib打包到Harbor镜像仓库
什么是Jib
Jib 是 Google 开发的可以直接构建 Java 应用的 Docker 和 OCI 镜像的类库,以 Maven 和 Gradle 插件形式提供。
Jib带来的是,它允许您通过简单地将插件添加到您选择的构建工具(Maven或Gradle)来创建容器,没有额外的文件,只需几行配置,它处理将应用程序打包到容器映像的所有步骤。
Jib是来自Google的开源Java容器,它允许Java开发人员使用他们所知道的Java工具构建容器,它不需要您编写Dockerfile或安装了docker,它直接集成到Maven和Gradle中。
pom文件配置jib
对于应用程序的基本本地存储镜像,请在pom.xml以下内容中配置jib-maven-plugin
公共属性配置
在properties中配置
harbor
的共有配置
<properties>
<!--harbor 仓库地址-->
<docker.registry.url>itcastharbor.com</docker.registry.url>
<!--harbor 的项目名称-->
<docker.registry.name>library</docker.registry.name>
<!--harbor账号-->
<docker.registry.username>itcast</docker.registry.username>
<!--harbor密码-->
<docker.registry.password>Qwert123</docker.registry.password>
</properties>
编译配置插件配置
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>2.8.0</version>
<configuration>
<allowInsecureRegistries>true</allowInsecureRegistries>
<!--from节点用来设置镜像的基础镜像,相当于Docerkfile中的FROM关键字-->
<from>
<!--使用openjdk官方镜像,tag是:8-jdk-alpine,表示镜像的操作系统是alpine,装好了jdk8-->
<image>openjdk:8-jdk-alpine</image>
</from>
<to>
<!--镜像名称和tag,使用了mvn内置变量${project.version},表示当前工程的version-->
<image>${docker.registry.url}/${docker.registry.name}/${project.artifactId}:${project.version}
</image>
<tags>
<!--版本号-->
<tag>${project.version}</tag>
</tags>
<!--harbor的认证信息-->
<auth>
<username>${docker.registry.username}</username>
<password>${docker.registry.password}</password>
</auth>
</to>
<!--容器相关的属性-->
<container>
<jvmFlags>
<!--一些启动参数-->
<jvmFlag>-Djava.security.edg=file:/dev/./urandom</jvmFlag>
</jvmFlags>
<!--挂载volume的配置-->
<volumes>
<volume>/tmp</volume>
<volume>/logs</volume>
</volumes>
<ports>
<!--暴漏端口号-->
<port>8080</port>
</ports>
<!--微服务的启动类-->
<mainClass>com.heima.test.Application</mainClass>
<format>OCI</format>
<!--使用该参数将镜像的创建时间与系统时间对其-->
<creationTime>USE_CURRENT_TIMESTAMP</creationTime>
</container>
</configuration>
<executions>
<!--执行打包配置-->
<execution>
<id>jib-maven-plugin</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Docker maven plugin -->
</plugins>
</build>
执行构建
然后在项目根目录执行
mvn clean compile jib:build
就可以了
仓库中查看镜像
在我们的仓库中查看镜像,我们看到镜像都已经上传到仓库中了