音效素材网提供各类素材,打造精品素材网站!

站内导航 站长工具 投稿中心 手机访问

音效素材

使用jib打包docker镜像实战
日期:2018-08-27 08:31:53   来源:脚本之家


本文主要研究一下如何使用google的jib打包docker镜像

Jib 是谷歌最新开源的 Java 应用的 Docker 镜像生成工具,可以通过 Gradle 或 Maven 直接生成镜像并上传到仓库而不需要 Dockerfile 文件或者其他插件;Jib 支持将资源文件和类分层打包,可以大幅度提升生成镜像的速度

有一些其他的插件也可以通过 Docker 实现生成镜像,如com.palantir.docker等,但是都需要额外配置 Dockerfile, 如果应用仅需要通过 Dockerfile 构建镜像,建议使用 Jib 来提升构建和上传速度

maven

      <plugin>
        <groupId>com.google.cloud.tools</groupId>
        <artifactId>jib-maven-plugin</artifactId>
        <version>0.9.9</version>
      </plugin>

配置

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <maven.build.timestamp.format>yyyyMMdd</maven.build.timestamp.format>
  </properties>


  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>com.google.cloud.tools</groupId>
        <artifactId>jib-maven-plugin</artifactId>
        <version>0.9.9</version>
        <configuration>
          <container>
            <jvmFlags>
              <jvmFlag>-Xms512m</jvmFlag>
            </jvmFlags>
            <ports>
              <port>8080</port>
            </ports>
            <useCurrentTimestamp>true</useCurrentTimestamp>
          </container>
          <from>
            <image>java:8u172-jre-alpine</image>
          </from>
          <to>
            <image>jib-demo:${maven.build.timestamp}</image>
          </to>
          <allowInsecureRegistries>true</allowInsecureRegistries>
          <extraDirectory>${project.basedir}/src/main/jib</extraDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>

这里指定base image为java:8u172-jre-alpine

同时指定生成的image名称为jib-demo:${maven.build.timestamp}

container标签可以指定相关参数,比如jvmFlags指定JVM参数,ports指定expose的端口号

extraDirectory用于指定要添加的目录,默认为${project.basedir}/src/main/jib,即该目录下的文件夹会在docker镜像里头同步创建,目录层级关系以jib下目录层级关系为准。比如该目录下有app/resources/demo.txt,则对应该镜像目录为/app/resources/demo.txt

打包

mvn compile jib:dockerBuild -X

使用jib:dockerBuild是在本地打包镜像,不push到远程,-X参数是显示debug信息

如果使用jib:build命令,则打包之后push到远程

输出日志实例:

[DEBUG] TIMING  Building image to Docker daemon
[DEBUG] RUNNING  Building image to Docker daemon
[INFO] Getting base image java:8u172-jre-alpine...
[DEBUG] TIMING  Pulling base image manifest
[DEBUG] RUNNING  Pulling base image manifest
[DEBUG] TIMING  Building application layers
[DEBUG] RUNNING  Building application layers
[INFO] Building dependencies layer...
[DEBUG] TIMING  Building dependencies layer
[DEBUG] RUNNING  Building dependencies layer
[INFO] Building resources layer...
[DEBUG] TIMING  Building resources layer
[DEBUG] RUNNING  Building resources layer
[INFO] Building classes layer...
[DEBUG] TIMING  Building classes layer
[DEBUG] RUNNING  Building classes layer
[INFO] Building extra files layer...
[DEBUG] TIMING  Building extra files layer
[DEBUG] RUNNING  Building extra files layer
[DEBUG] TIMED  Building application layers : 3.498 ms
[DEBUG] Building application layers : 3.498 ms
[DEBUG] Building extra files layer built sha256:0a9321c621ee2c138d9a93f4ba47f825e20dd7aca0e9206f30bf560122fdaa31
[DEBUG] Building resources layer built sha256:933116c8da4db70000355b750af2f3b5c4545e74db7b5eb0a5e37689ef86d44f
[DEBUG] Building classes layer built sha256:a99dbde7d015ba3d15b9265ac38f6536d65b00dd2a864b7003a34742f483939b
[DEBUG] TIMED  Building extra files layer : 33.718 ms
[DEBUG] TIMED  Building classes layer : 33.901 ms
[DEBUG] Building extra files layer : 33.718 ms
[DEBUG] TIMED  Building resources layer : 34.062 ms
[DEBUG] Building resources layer : 34.062 ms
[DEBUG] Building classes layer : 33.901 ms
[DEBUG] Building dependencies layer built sha256:a19961ec08095eb70ca3a070945223dcf51500a255a745272276139bf0a29f4e
[DEBUG] TIMED  Building dependencies layer : 1047.83 ms
[DEBUG] Building dependencies layer : 1047.83 ms
[INFO] The base image requires auth. Trying again for java:8u172-jre-alpine...
//......
[DEBUG] TIMED  Pulling base image manifest : 8959.891 ms
[DEBUG] Pulling base image manifest : 8959.891 ms
[DEBUG] TIMING  Setting up base image caching
[DEBUG] RUNNING  Setting up base image caching
[DEBUG] TIMED  Setting up base image caching : 0.824 ms
[DEBUG] Setting up base image caching : 0.824 ms
[DEBUG] TIMING  Pulling base image layer sha256:500c0ac4cdc58aee9a008e70466eeec8a4b74b25c29d003764635d2fe1b42827
[DEBUG] RUNNING  Pulling base image layer sha256:500c0ac4cdc58aee9a008e70466eeec8a4b74b25c29d003764635d2fe1b42827
[DEBUG] TIMING  Pulling base image layer sha256:aa38cd8c77d7320c7489327775e074580db5f23467198aab3e79c7db5cdfbdca
[DEBUG] TIMING  Pulling base image layer sha256:cb56d62302ec31640b40ca15843e7af6be145966a1064c78ea7afd6f10ce49e6
[DEBUG] RUNNING  Pulling base image layer sha256:cb56d62302ec31640b40ca15843e7af6be145966a1064c78ea7afd6f10ce49e6
[DEBUG] TIMING  Pulling base image layer sha256:ff3a5c916c92643ff77519ffa742d3ec61b7f591b6b7504599d95a4a41134e28
[DEBUG] RUNNING  Pulling base image layer sha256:ff3a5c916c92643ff77519ffa742d3ec61b7f591b6b7504599d95a4a41134e28
[DEBUG] RUNNING  Pulling base image layer sha256:aa38cd8c77d7320c7489327775e074580db5f23467198aab3e79c7db5cdfbdca
[DEBUG] TIMED  Pulling base image layer sha256:500c0ac4cdc58aee9a008e70466eeec8a4b74b25c29d003764635d2fe1b42827 : 612.524 ms
[DEBUG] Pulling base image layer sha256:500c0ac4cdc58aee9a008e70466eeec8a4b74b25c29d003764635d2fe1b42827 : 612.524 ms
[DEBUG] TIMED  Pulling base image layer sha256:ff3a5c916c92643ff77519ffa742d3ec61b7f591b6b7504599d95a4a41134e28 : 9985.702 ms
[DEBUG] Pulling base image layer sha256:ff3a5c916c92643ff77519ffa742d3ec61b7f591b6b7504599d95a4a41134e28 : 9985.702 ms
[DEBUG] TIMED  Pulling base image layer sha256:aa38cd8c77d7320c7489327775e074580db5f23467198aab3e79c7db5cdfbdca : 27267.144 ms
[DEBUG] Pulling base image layer sha256:aa38cd8c77d7320c7489327775e074580db5f23467198aab3e79c7db5cdfbdca : 27267.144 ms
[DEBUG] TIMED  Pulling base image layer sha256:cb56d62302ec31640b40ca15843e7af6be145966a1064c78ea7afd6f10ce49e6 : 54734.971 ms
[DEBUG] Pulling base image layer sha256:cb56d62302ec31640b40ca15843e7af6be145966a1064c78ea7afd6f10ce49e6 : 54734.971 ms
[DEBUG] TIMING  Building container configuration
[DEBUG] RUNNING  Building container configuration
[INFO] Finalizing...
[DEBUG] TIMED  Building container configuration : 0.462 ms
[DEBUG] Building container configuration : 0.462 ms
[INFO] Loading to Docker daemon...
[DEBUG] TIMED  Building image to Docker daemon : 65862.956 ms
[DEBUG] Building image to Docker daemon : 65862.956 ms
[INFO]
[INFO] Container entrypoint set to [java, -Xms512m, -cp, /app/resources/:/app/classes/:/app/libs/*, com.example.JibDemoApplication]
[INFO]
[INFO] Built image to Docker daemon as jib-demo:20180826
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:07 min
[INFO] Finished at: 2018-08-26T21:38:11+08:00
[INFO] Final Memory: 26M/493M
[INFO] ------------------------------------------------------------------------

查看镜像

docker inspect jib-demo:20180826
[
  {
    "Id": "sha256:7554492dde84091bc6d015b29ec4bf854bb841b9ec4d8f7bef8b51a8ec59ef94",
    "RepoTags": [
      "jib-demo:20180826"
    ],
    "RepoDigests": [],
    "Parent": "",
    "Comment": "",
    "Created": "2018-08-26T13:37:05.65Z",
    "Container": "",
    "ContainerConfig": {
      "Hostname": "",
      "Domainname": "",
      "User": "",
      "AttachStdin": false,
      "AttachStdout": false,
      "AttachStderr": false,
      "Tty": false,
      "OpenStdin": false,
      "StdinOnce": false,
      "Env": null,
      "Cmd": null,
      "Image": "",
      "Volumes": null,
      "WorkingDir": "",
      "Entrypoint": null,
      "OnBuild": null,
      "Labels": null
    },
    "DockerVersion": "",
    "Author": "",
    "Config": {
      "Hostname": "",
      "Domainname": "",
      "User": "",
      "AttachStdin": false,
      "AttachStdout": false,
      "AttachStderr": false,
      "ExposedPorts": {
        "8080/tcp": {}
      },
      "Tty": false,
      "OpenStdin": false,
      "StdinOnce": false,
      "Env": [
        "PATH=/opt/jdk1.8.0_172/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
        "JAVA_HOME=/opt/jdk1.8.0_172"
      ],
      "Cmd": [],
      "Image": "",
      "Volumes": null,
      "WorkingDir": "",
      "Entrypoint": [
        "java",
        "-Xms512m",
        "-cp",
        "/app/resources/:/app/classes/:/app/libs/*",
        "com.example.JibDemoApplication"
      ],
      "OnBuild": null,
      "Labels": {}
    },
    "Architecture": "amd64",
    "Os": "linux",
    "Size": 186079594,
    "VirtualSize": 186079594,
    "GraphDriver": {
      "Data": null,
      "Name": "aufs"
    },
    "RootFS": {
      "Type": "layers",
      "Layers": [
        "sha256:cd7100a72410606589a54b932cabd804a17f9ae5b42a1882bd56d263e02b6215",
        "sha256:317e0b42ef29551507adc792c2dbc389af85c5d68900cf0f08abe907cbd4e199",
        "sha256:d6fd5302a9ee64c43b46153e9678153819d6d0e247381acbbe6cbe6ed9de0366",
        "sha256:417c6bd0930eb69d17ae5d601898a6a012900ca33c69e612840a3042b2451cbb",
        "sha256:8309a5eed5158c5639acb664312ddc7027a2ecb172744f548d6d9fb5f918a744",
        "sha256:6ef8c795d7a5e2a07f51d572a90b820e089f89adf30798193f4f3f410a261415",
        "sha256:9990447f3d37e3f2dc70718d0ea6602cf7843091d8bbe395f552c9c5dd10551c",
        "sha256:ff7910aaeee962d1d86e00742030378a2758da59d8c8cda3fe44314bbddd8081"
      ]
    },
    "Metadata": {
      "LastTagTime": "0001-01-01T00:00:00Z"
    }
  }
]

可以看到java -cp指定了/app/libs/、/app/resources/、/app/classes/这三个文件夹

小结

jib默认执行如下操作

COPY libs /app/libs/
COPY resources /app/resources/
COPY classes /app/classes/

也就是将libs拷贝到/app/libs,将resources拷贝到/app/resources,将classes拷贝到/app/classes.

doc

  • jib
  • Building Docker Image for a Spring Boot App With Jib
  • build-containers-faster-with-jib-a-google-image-build-tool-for-java-applications

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

    您感兴趣的教程

    在docker中安装mysql详解

    本篇文章主要介绍了在docker中安装mysql详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编...

    详解 安装 docker mysql

    win10中文输入法仅在桌面显示怎么办?

    win10中文输入法仅在桌面显示怎么办?

    win10系统使用搜狗,QQ输入法只有在显示桌面的时候才出来,在使用其他程序输入框里面却只能输入字母数字,win10中...

    win10 中文输入法

    一分钟掌握linux系统目录结构

    这篇文章主要介绍了linux系统目录结构,通过结构图和多张表格了解linux系统目录结构,感兴趣的小伙伴们可以参考一...

    结构 目录 系统 linux

    PHP程序员玩转Linux系列 Linux和Windows安装

    这篇文章主要为大家详细介绍了PHP程序员玩转Linux系列文章,Linux和Windows安装nginx教程,具有一定的参考价值,感兴趣...

    玩转 程序员 安装 系列 PHP

    win10怎么安装杜比音效Doby V4.1 win10安装杜

    第四代杜比®家庭影院®技术包含了一整套协同工作的技术,让PC 发出清晰的环绕声同时第四代杜比家庭影院技术...

    win10杜比音效

    纯CSS实现iOS风格打开关闭选择框功能

    这篇文章主要介绍了纯CSS实现iOS风格打开关闭选择框,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作...

    css ios c

    Win7如何给C盘扩容 Win7系统电脑C盘扩容的办法

    Win7如何给C盘扩容 Win7系统电脑C盘扩容的

    Win7给电脑C盘扩容的办法大家知道吗?当系统分区C盘空间不足时,就需要给它扩容了,如果不管,C盘没有足够的空间...

    Win7 C盘 扩容

    百度推广竞品词的投放策略

    SEM是基于关键词搜索的营销活动。作为推广人员,我们所做的工作,就是打理成千上万的关键词,关注它们的质量度...

    百度推广 竞品词

    Visual Studio Code(vscode) git的使用教程

    这篇文章主要介绍了详解Visual Studio Code(vscode) git的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...

    教程 Studio Visual Code git

    七牛云储存创始人分享七牛的创立故事与

    这篇文章主要介绍了七牛云储存创始人分享七牛的创立故事与对Go语言的应用,七牛选用Go语言这门新兴的编程语言进行...

    七牛 Go语言

    Win10预览版Mobile 10547即将发布 9月19日上午

    微软副总裁Gabriel Aul的Twitter透露了 Win10 Mobile预览版10536即将发布,他表示该版本已进入内部慢速版阶段,发布时间目...

    Win10 预览版

    HTML标签meta总结,HTML5 head meta 属性整理

    移动前端开发中添加一些webkit专属的HTML5头部标签,帮助浏览器更好解析HTML代码,更好地将移动web前端页面表现出来...

    移动端html5模拟长按事件的实现方法

    这篇文章主要介绍了移动端html5模拟长按事件的实现方法的相关资料,小编觉得挺不错的,现在分享给大家,也给大家...

    移动端 html5 长按

    HTML常用meta大全(推荐)

    这篇文章主要介绍了HTML常用meta大全(推荐),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参...

    cdr怎么把图片转换成位图? cdr图片转换为位图的教程

    cdr怎么把图片转换成位图? cdr图片转换为

    cdr怎么把图片转换成位图?cdr中插入的图片想要转换成位图,该怎么转换呢?下面我们就来看看cdr图片转换为位图的...

    cdr 图片 位图

    win10系统怎么录屏?win10系统自带录屏详细教程

    win10系统怎么录屏?win10系统自带录屏详细

    当我们是使用win10系统的时候,想要录制电脑上的画面,这时候有人会想到下个第三方软件,其实可以用电脑上的自带...

    win10 系统自带录屏 详细教程

    + 更多教程 +
    WIN服务器linux服务器FTP服务器DNS服务器其他