jib Official:GoogleContainerTools/jib https://github.com/GoogleContainerTools/jib
本文示例完整 demo github 地址 https://github.com/moxingwang/jib https://github.com/moxingwang/jib
想要了解并且使用 jib, 首先你得知道或者使用过 docker, 在这个基础上才能够明白 jib 是干什么得, 解决了什么问题.
首先我们先看一个例子, 如何将一个 spring-boot 项目简单容器化, 如果你从未操作过, 可以动手自己实现, 或者仔细阅读这篇文章第一个 spring Boot 应用通过 Docker 来实现构建, 运行, 发布 https://blog.csdn.net/u010046908/article/details/56008445 . 你可能发现如果需要把一个 java 项目容器化这是一个麻烦和相对复杂的过程, 首先你需要编写 dockerfile, 然后在 build dockefile, 如过需要 push 到 registries, 还需要经过 push 操作. 虽然 build 和 push 还是非常简单得. 但是写一个 dockerfile 真的比较麻烦, 学习成本也高, 看看官网 Dockerfile reference https://docs.docker.com/engine/reference/builder/#usage 就不想学了. 那么有没有简单得办法呢, 记住几个简单得命令就可以运行了, 如同'mvn spring-boot:run'这么简单得命令, 这个时候 jib 出场了, 他就是来完成这个伟大得使命.
如果你还没明白 jib 是用来做什么事情得, 推荐你阅读谷歌开源 Java 镜像构建工具 Jib http://www.infoq.com/cn/news/2018/07/google-opensource-Jib 的前半部分, 或者阅读 jib Official:GoogleContainerTools/jib https://github.com/GoogleContainerTools/jib .
接下来主要介绍具体如何使用 (maven 的方式).
将 java 项目容器化 push 到 registries
jib 的引入非常简单, 已 maven plugin 的方式集成到项目中, 只需要在你的项目中引入下面代码即可
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>0.9.6</version>
<configuration>
<from>
<!--base image-->
<image>openjdk:alpine</image>
</from>
<to>
<!--<image>registry.cn-hangzhou.aliyuncs.com/m65536/jibtest</image>-->
<!-- 目标镜像 registry 地址, 为了方便测试, 你需要换成自己的地址, 如果你的网络不好, 可以选用国内加速器, 比如阿里云的 -->
<image>registry.hub.docker.com/moxingwang/jibtest</image>
</to>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
这只是一个最简单的配置, 比如 registry 认证配置, jvm 配置等等, 可以参考 github jib 详细说明 jib/jib-maven-plugin/ https://github.com/GoogleContainerTools/jib/tree/master/jib-maven-plugin#from-object .
构建你的镜像
mvn compile jib:build
可能你刚刚运行玩这一步就出现错误, 提示 Unauthorized, 不用担心, 这个命令需要把构建出来的 image commit 并且 push 到你远程的 registry(我这里是 registry.hub.docker.com), 你可以按照 [常见错误 1] 的方法解决问题.
如果你的网速慢一点可能会在最后步骤'Finalizing...', 不过没有关系, 稍微多等一会就 ok, 直到'BUILD SUCCESS'出现. 最终一个完整的 java project 被实例化成镜像并且 push 到了 registry 里. 接下来你可以测试刚刚完成 push 的镜像.
打开 https://hub.docker.com/ https://hub.docker.com/ , 你会发现你的 image 已经构建成功.
docker 使用镜像
docker run -it --rm -p8080:8080 registry.hub.docker.com/moxingwang/jibtest
一个 spring-boot 的 image 这样就被启动成功了. 本文示例 demo 中集成了 swagger-iu, 项目启动成功后我们可以通过 http://localhost:8080/sw/swagger-ui.html http://localhost:8080/sw/swagger-ui.html 访问, 可以成功在浏览器打开 swagger.
把镜像直接构建到本地 docker 中
使用这种构建方式, 首先你要确保本已经安装 docker 并且环境变量都配置正确, 因为这种方式 jib 依赖 docker 命令执行, 你还需要保证已经启动服务 (在 terminal 中执行 docker --version 能够正确返回).
mvn compile jib:dockerBuild
接下来直接操作, 首先查看本地已有 images
docker iamges
执行 mvn compile jib:dockerBuild
可以看到本地 image 已经 build 成功. 可以直接启动使用
docker run -it --rm -p8080:8080 moxingwang/jibtest
启动成功后同样可以直接访问 http://localhost:8080/sw/swagger-ui.html http://localhost:8080/sw/swagger-ui.html swagger 页面.
常见错误
常见错误 1 Failed to execute goal com.google.cloud.tools:jib-maven-plugin:0.9.6:build (default-cli) on project jib: Build image failed, perhaps you should set a credential helper name with the configuration '<from><credHelper>' or set credentials for 'registry.hub.docker.com' in your Maven settings: Unauthorized for registry.hub.docker.com/moxingwang/jibtest: 401 Unauthorized
当你从私有的 registries pull 或者 push 的时候需要认证机制. 阅读官方说明按照步骤操作即可. Authentication Methods https://github.com/GoogleContainerTools/jib/blob/master/jib-maven-plugin/README.md .
如果帮你本机已经按照 docker, 那么操作更简单, 可以通过 docker 命令直接登陆 registries, 然后继续其他 jib 的操作.
registry.hub.docker.com
docker login --username = 你的 hub.docker 的用户名 registry.hub.docker.com
registry.hub.docker.com
docker login --username = 阿里云 docker 用户名 registry.cn-hangzhou.aliyuncs.com
来源: http://blog.51cto.com/13828850/2146314