Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Building Docker Image for a Spring Boot App With Jib

DZone's Guide to

Building Docker Image for a Spring Boot App With Jib

Want to learn how to build a Docker image for a Spring Boot app? Check out this post to learn how with Jib.

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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 explain Jib with an approach that I was using before.

I was creating Docker images using bmuschko's excellent Gradle-Docker plugin. After gaining 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 derived from "java:8" base image:

FROM java:8
...


The 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 705 MB on my machine. Again, this has nothing to do with the Gradle-Docker plugin, but this is based on my personal 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'
}


Next, you will need to 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 about 150 MB in size.

If I had access to a Docker registry, 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. 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.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ,docker ,docker image ,tutorial ,spring boot ,jibs ,jib ,gradle-docker plugin

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}