Jib构建你的第一个java镜像

<div data-v-7154d244="" data-id="5b4e9ca35188251b176a5ef1" itemprop="articleBody" class="article-content"><blockquote> <p><a target="_blank" href="https://link.juejin.im?target=https%3A%2F%2Fgithub.com%2FGoogleContainerTools%2Fjib" rel="nofollow noopener noreferrer">jib Official:GoogleContainerTools/jib</a></p> </blockquote> <blockquote> <p>本文示例完整demo github地址 <a target="_blank" href="https://link.juejin.im?target=https%3A%2F%2Fgithub.com%2Fmoxingwang%2Fjib" rel="nofollow noopener noreferrer">github.com/moxingwang/…</a></p> </blockquote> <blockquote> <p>想要了解而且使用jib,首先你得知道或者使用过docker,在这个基础上才可以明白jib是干什么得,解决了什么问题。</p> </blockquote> <p>首先咱们先看一个例子,如何将一个spring-boot项目简单容器化,若是你从未操做过,能够动手本身实现,或者仔细阅读这篇文章<a target="_blank" href="https://link.juejin.im?target=https%3A%2F%2Fblog.csdn.net%2Fu010046908%2Farticle%2Fdetails%2F56008445" rel="nofollow noopener noreferrer">第一个 spring Boot 应用经过Docker 来实现构建、运行、发布</a>。你可能发现若是须要把一个java项目容器化这是一个麻烦和相对复杂的过程,首先你须要编写dockerfile,而后在build dockefile,如过须要push到registries,还须要通过push操做。虽然build和push仍是很是简单得。可是写一个dockerfile真的比较麻烦,学习成本也高,看看官网<a target="_blank" href="https://link.juejin.im?target=https%3A%2F%2Fdocs.docker.com%2Fengine%2Freference%2Fbuilder%2F%23usage" rel="nofollow noopener noreferrer">Dockerfile reference</a>就不想学了。那么有没有简单得办法呢,记住几个简单得命令就能够运行了,如同‘mvn spring-boot:run’这么简单得命令,这个时候jib出场了,他就是来完成这个伟大得使命。</p> <p>若是你还没明白jib是用来作什么事情得,推荐你阅读<a target="_blank" href="https://link.juejin.im?target=http%3A%2F%2Fwww.infoq.com%2Fcn%2Fnews%2F2018%2F07%2Fgoogle-opensource-Jib" rel="nofollow noopener noreferrer">谷歌开源Java镜像构建工具Jib</a>的前半部分,或者阅读<a target="_blank" href="https://link.juejin.im?target=https%3A%2F%2Fgithub.com%2FGoogleContainerTools%2Fjib" rel="nofollow noopener noreferrer">jib Official:GoogleContainerTools/jib</a>。</p> <p>接下来主要介绍具体如何使用(maven的方式)。</p> <h1 class="heading" data-id="heading-0">将java项目容器化push到registries</h1> <p>jib的引入很是简单,已maven plugin的方式集成到项目中,只须要在你的项目中引入下面代码便可</p> <pre><code class="hljs bash copyable" lang="bash">&lt;plugin&gt; &lt;groupId&gt;com.google.cloud.tools&lt;/groupId&gt; &lt;artifactId&gt;jib-maven-plugin&lt;/artifactId&gt; &lt;version&gt;0.9.6&lt;/version&gt; &lt;configuration&gt; &lt;from&gt; &lt;!--base image--&gt; &lt;image&gt;openjdk:alpine&lt;/image&gt; &lt;/from&gt; &lt;to&gt; &lt;!--&lt;image&gt;registry.cn-hangzhou.aliyuncs.com/m65536/jibtest&lt;/image&gt;--&gt; &lt;!--目标镜像registry地址,为了方便测试,你须要换成本身的地址,若是你的网络很差,能够选用国内加速器,好比阿里云的--&gt; &lt;image&gt;registry.hub.docker.com/moxingwang/jibtest&lt;/image&gt; &lt;/to&gt; &lt;/configuration&gt; &lt;executions&gt; &lt;execution&gt; &lt;phase&gt;package&lt;/phase&gt; &lt;goals&gt; &lt;goal&gt;build&lt;/goal&gt; &lt;/goals&gt; &lt;/execution&gt; &lt;/executions&gt; &lt;/plugin&gt;html

<span class="copy-code-btn">复制代码</span></code></pre><p>这只是一个最简单的配置,好比registry认证配置,jvm配置等等,能够参考github jib详细说明<a target="_blank" href="https://link.juejin.im?target=https%3A%2F%2Fgithub.com%2FGoogleContainerTools%2Fjib%2Ftree%2Fmaster%2Fjib-maven-plugin%23from-object" rel="nofollow noopener noreferrer">jib/jib-maven-plugin/</a>。</p>java

<h3 class="heading" data-id="heading-1">构建你的镜像</h3> <pre><code class="hljs bash copyable" lang="bash">mvn compile jib:build <span class="copy-code-btn">复制代码</span></code></pre><p>可能你刚刚运行玩这一步就出现错误,提示Unauthorized,不用担忧,这个命令须要把构建出来的image commit而且push到你远程的registry(我这里是registry.hub.docker.com),你能够按照【常见错误1】的方法解决问题。</p> <p>若是你的网速慢一点可能会在最后步骤'Finalizing...',不过没有关系,稍微多等一会就ok,直到'BUILD SUCCESS'出现。最终一个完整的java project被实例化成镜像而且push到了registry里。接下来你能够测试刚刚完成push的镜像。</p> <p>打开<a target="_blank" href="https://link.juejin.im?target=https%3A%2F%2Fhub.docker.com%2F" rel="nofollow noopener noreferrer">hub.docker.com/</a>,你会发现你的image已经构建成功。</p> <p></p><figure><img class="lazyload inited loaded" data-src="https://user-gold-cdn.xitu.io/2018/7/18/164ab12624060e6a?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" data-width="1280" data-height="505" src="https://user-gold-cdn.xitu.io/2018/7/18/164ab12624060e6a?imageView2/0/w/1280/h/960/format/webp/ignore-error/1"><figcaption></figcaption></figure><p></p> <ul> <li>docker使用镜像</li> </ul> <pre><code class="hljs bash copyable" lang="bash">docker run -it --rm -p8080:8080 registry.hub.docker.com/moxingwang/jibtest <span class="copy-code-btn">复制代码</span></code></pre><p></p><figure><img class="lazyload inited loaded" data-src="https://user-gold-cdn.xitu.io/2018/7/18/164ab1262b1552fd?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" data-width="1280" data-height="757" src="https://user-gold-cdn.xitu.io/2018/7/18/164ab1262b1552fd?imageView2/0/w/1280/h/960/format/webp/ignore-error/1"><figcaption></figcaption></figure><p></p> <p>一个spring-boot的image这样就被启动成功了。本文示例demo中集成了swagger-iu, 项目启动成功后咱们能够经过<a target="_blank" href="https://link.juejin.im?target=http%3A%2F%2Flocalhost%3A8080%2Fsw%2Fswagger-ui.html" rel="nofollow noopener noreferrer">http://localhost:8080/sw/swagger-ui.html</a>访问,能够成功在浏览器打开swagger。</p> <p></p><figure><img class="lazyload inited loaded" data-src="https://user-gold-cdn.xitu.io/2018/7/18/164ab12624440109?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" data-width="1280" data-height="430" src="https://user-gold-cdn.xitu.io/2018/7/18/164ab12624440109?imageView2/0/w/1280/h/960/format/webp/ignore-error/1"><figcaption></figcaption></figure><p></p> <h3 class="heading" data-id="heading-2">把镜像直接构建到本地docker中</h3> <p>使用这种构建方式,首先你要确保本已经安装docker而且环境变量都配置正确,由于这种方式jib依赖docker 命令执行,你还须要保证已经启动服务(在terminal中执行 docker --version 可以正确返回)。</p> <pre><code class="hljs bash copyable" lang="bash">mvn compile jib:dockerBuild <span class="copy-code-btn">复制代码</span></code></pre><p>接下来直接操做,首先查看本地已有images</p> <pre><code class="hljs bash copyable" lang="bash">docker iamges <span class="copy-code-btn">复制代码</span></code></pre><p></p><figure><img class="lazyload inited" data-src="https://user-gold-cdn.xitu.io/2018/7/18/164ab1262a968e52?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" data-width="1280" data-height="677" src="https://img2018.cnblogs.com/blog/1112483/201905/1112483-20190529144513207-1445009970.png"><figcaption></figcaption></figure><p></p> <p>执行mvn compile jib:dockerBuild</p> <p></p><figure><img class="lazyload inited" data-src="https://user-gold-cdn.xitu.io/2018/7/18/164ab1262a69bcf4?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" data-width="1280" data-height="680" src="https://user-gold-cdn.xitu.io/2018/7/18/164ab1262a69bcf4?imageView2/0/w/1280/h/960/format/webp/ignore-error/1"><figcaption></figcaption></figure><p></p> <p>能够看到本地image已经build成功。能够直接启动使用</p> <pre><code class="hljs bash copyable" lang="bash">docker run -it --rm -p8080:8080 moxingwang/jibtest <span class="copy-code-btn">复制代码</span></code></pre><p>启动成功后一样能够直接访问<a target="_blank" href="https://link.juejin.im?target=http%3A%2F%2Flocalhost%3A8080%2Fsw%2Fswagger-ui.html" rel="nofollow noopener noreferrer">http://localhost:8080/sw/swagger-ui.html</a>swagger页面。</p> <h1 class="heading" data-id="heading-3">常见错误</h1> <h4 class="heading" data-id="heading-4">常见错误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 '' or set credentials for 'registry.hub.docker.com' in your Maven settings: Unauthorized for registry.hub.docker.com/moxingwang/jibtest: 401 Unauthorized</h4> <blockquote> <p>当你从私有的registries pull或者push的时候须要认证机制。阅读官方说明按照步骤操做便可。<a target="_blank" href="https://link.juejin.im?target=https%3A%2F%2Fgithub.com%2FGoogleContainerTools%2Fjib%2Fblob%2Fmaster%2Fjib-maven-plugin%2FREADME.md" rel="nofollow noopener noreferrer">Authentication Methods</a>。 若是帮你本机已经按照docker,那么操做更简单,能够经过docker命令直接登录registries,而后继续其余jib的操做。</p> </blockquote> <ul> <li>registry.hub.docker.com</li> </ul> <pre><code class="hljs bash copyable" lang="bash">docker login --username=你的hub.docker的用户名 registry.hub.docker.com <span class="copy-code-btn">复制代码</span></code></pre><ul> <li>registry.cn-hangzhou.aliyuncs.com</li> </ul> <pre><code class="hljs bash copyable" lang="bash">docker login --username=阿里云docker用户名 registry.cn-hangzhou.aliyuncs.com <span class="copy-code-btn">复制代码</span></code></pre><h1 class="heading" data-id="heading-5">推荐阅读</h1> <ul> <li><a target="_blank" href="https://link.juejin.im?target=https%3A%2F%2Fgithub.com%2FGoogleContainerTools%2Fjib" rel="nofollow noopener noreferrer">jib Official:GoogleContainerTools/jib</a></li> <li><a target="_blank" href="https://link.juejin.im?target=http%3A%2F%2Fwww.infoq.com%2Fcn%2Fnews%2F2018%2F07%2Fgoogle-opensource-Jib" rel="nofollow noopener noreferrer">谷歌开源Java镜像构建工具Jib</a></li> </ul> </div> 原文地址:https://juejin.im/post/5b4e9c316fb9a04fa01d39d6git

相关文章
相关标签/搜索