80

Jib - Building docker image for a Spring Boot App

 5 years ago
source link: https://www.tuicool.com/articles/hit/vYb6jui
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

I was pleasantly surprised by how easy it was to create a docker image for a sample Spring Boot application using Jib .

Let me first contrast Jib with an approach that I was using before.

I was creating docker images using bmuschko's excellent gradle-docker plugin. Given access to a docker daemon and a gradle dsl based description of the Dockerfile or a straight Dockerfile, it would create the docker image using a gradle task. In my case, the task to create the docker image looks something like this:

task createDockerImage(type: DockerBuildImage) {
    inputDir = file('.')
    dockerFile = project.file('docker/Dockerfile')
    tags = ['sample-micrometer-app:' + project.version]
}

createDockerImage.dependsOn build

and my Dockerfile itself derived off "java:8" base image:

FROM java:8
...

gradle-docker-plugin made it simple to create a docker image right from gradle with the catch that the plugin needs access to a docker daemon to create the image. Also since the base "java:8" image is large the final docker image turns out to be around 705MB on my machine. Again no fault of the gradle-docker plugin but based on my choice of base image.

Now with Jib, all I have to do is to add the plugin :

plugins {
    id 'com.google.cloud.tools.jib' version '0.9.6'
}

Configure it to give the image a name:

jib {
    to {
        image = "sample-micrometer-app:0.0.1-SNAPSHOT"
    }
}

And that is it. With a local docker daemon available, I can create my docker image using the following task:

./gradlew jibDockerBuild

Jib automatically selects a very lightweight base image - my new image is just about 150 MB in size.

If I had access to a docker registry available then the local docker daemon is not required, it can directly create and publish the image to a docker registry!

Jib gradle plugin provides an interesting task - "jibExportDockerContext" to export out the docker file, this way if needed a docker build can be run using this Dockerfile, for my purposes I wanted to see the contents of this file and it looks something like this:

FROM gcr.io/distroless/java

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

ENTRYPOINT ["java","-cp","/app/libs/*:/app/resources/:/app/classes/","sample.meter.SampleServiceAppKt"]

All in all, a very smooth experience and Jib does live up to its goals . My sample project with jib integrated with a gradle build is available here .


Recommend

  • 30
    • 微信 mp.weixin.qq.com 5 years ago
    • Cache

    试水Jib

  • 6

    【把做大会】本文讲解了如何在不安装任何Docker客户端或使用Dockerfiles的情况下,通过使用Google的Jib为Spring Boot应用程序创建Docker或兼容OCI的镜像。在本文中,我们将学习如何在不安装Docker客户端或使用Dockerfile的情况下为我们的Spring...

  • 6

    Running Spring boot application as Native Image Reading Time: 6 minutes Hi all. In the last post on GraalVm we saw how polyglot functionality works when we are working with GraalVm. We saw how multiple languages...

  • 1
    • dzone.com 2 years ago
    • Cache

    Native-image With Spring Boot

    Native-image With Spring Boot Spring, welcome to the clou...

  • 0
    • qiankunli.github.io 1 year ago
    • Cache

    jib源码分析之Step实现

    阅读本文前,建议事先了解下 jib源码分析及应用 jib源码分析之细节 以常用...

  • 4
    • qiankunli.github.io 1 year ago
    • Cache

    jib源码分析之细节

    阅读本文前,建议事先了解下 jib源码分析及应用 通过docker registry v2 api,是可以上传镜像的 jib 的最后,是不是也是调用 docker registry v2 a...

  • 6
    • qiankunli.github.io 1 year ago
    • Cache

    jib源码分析及应用

    jib源码分析及应用 | 李乾坤的博客 阅读本文前,建议事先了解下

  • 4
    • blog.51cto.com 1 year ago
    • Cache

    Docker与Jib(maven插件版)实战

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码): https://github.com/zq2599/blog_demos 关于Jib

  • 1

    Improving the Reproducibility of Spring Boot’s Docker Image Builder Craig Andrews

  • 1
    • blog.51cto.com 1 year ago
    • Cache

    Jib使用小结(Maven插件版)

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码): https://github.com/zq2599/blog_demos 近期在用Jib插件将Java...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK