Containerize your Spring Boot App with JIB plugin

Praveen G. Nair
5 min readJun 14, 2020
Dockerize spring boot apps with jib plugin.

Almost all the Java developers are familiar with building war or jar files, nd using their preferred tools, but in case of a microservice architecture, there would be certainly a need to build docker images.

Building any image is not always an easy task. Prerequisites includes minimum of docker knowledge, writing a docker file, running a docker daemon, and finally building and publishing the image to a registry.
There are several Maven and Gradle docker plugins that integrate with your app process like Spotify docker plugin and fabric8 docker plugin.

In this article, we are going to focus on Jib, an open-source container image builder developed by Google that uses a new approach. Jib allows for Java build containers using Maven or Gradle without a dockerfile or a docker daemon installed. That sounds pretty cool !! Is n’t it. Lets see How…

Why should I use Jib ?

The below quote is taken from the official page of Jib, which clearly stands apart and says its advantage.

You do not need to maintain a Dockerfile, run a Docker daemon, or even worry about creating a fat JAR with all its dependencies.

Jib takes advantage of image layering and registry caching to achieve fast, incremental builds. It reads your build config, organizes your application into distinct layers (dependencies, resources, classes) and only rebuilds and pushes the layers that have changed. When iterating quickly on a project, Jib can save valuable time on each build by only pushing your changed layers to the registry instead of your whole application.

Add Jib to your application

Now lets see an example.We are building a sample spring boot REST service and will be adding Jib dependency in it to create a Docker image for our project. Later we would see how we can push this image to our container registry of our choice.

We have a working project and now we can use Jib to create a container image for it. The Jib maven plugin will do all of the heavy lifting work. Add the plugin configuration to the <build><plugins> section of your pom.xml

Maven plugin for JIB
maven jib plugin

The only configuration we needed is to provide was <to><image>, which is the name of our Docker image. We can now use that plugin to create a Docker image that will be available on our local Docker daemon.

$ mvn clean compile jib:dockerBuild

mvn jib Build logs.

From the output, you can see that Jib has done it’s magic. It created three separate image layers for our application. Our default base layer is gcr.io/distroless/java:8 (more about distroless images). From there we get a separate image layer for our dependencies, our resources, and our classes. This means that changes to our application source code will only need to rebuild the classes layer. Jib also automatically discovered out main method in SpringBootJibApplication. Thats really cool !!

If we check Docker we can see that our new image is available for us to use.

$ docker images springboot-jib

docker images result

You will notice that CREATED says 50 years ago which doesn’t seem right. The reason for this that Jib creates reproducible builds by default; therefore, it does not include build timestamps. (more about reproducible builds.)

Let’s run to make sure it works.

$ docker run -p 8080:8080 --name springboot-jib springboot-jib

App Starting logs.
Success !!

All good !! Now we could easily push our image to Docker hub with a push command docker push ..., but Jib can also do that for us also. Also we you try to make some changes in your project and build again you would see a faster build going this time.

Jib Configuration for Registry

We can use Jib to automatically push our images to our container registry during a build. So, we need to tag our container image appropriately with the Docker repository path. We can tag our image with our maven project version as a best practice.

docker hub registry path updated.

Jib will need your container registry credentials to be able to push images for you. There are a few options to choose from, but I’m going to add my credentials inline with build command.

$ mvn jib:build -Djib.to.auth.username=prgnr173 -Djib.to.auth.password=

docker hub image pushed.

Additional Configuration in Jib Plugin

There are additional configurations which you can do with Jib. Like adding a FROM image of alpine if you are not interested to use a default distroless image.

using base image as alpine.

Jib supports numerous Java runtime configurations, too:

  1. jvmFlags is for indicating what startup flags to pass to the JVM.
  2. mainClass is for indicating the main class, which Jib will attempt to infer automatically by default.
  3. args is where we’d specify the program arguments passed to the main method.

Make sure to check out Jib’s documentation to see all the configuration properties available.

Comparison

Below is the build time comparison shared by Google Cloud where Jib stands a clear winner.

Build Time Jib vs Docker

Summary

In this blog, we have seen how to build and publish docker images using Google’s Jib. You can visit the official docs of Jib to customize even better according to your project requirements.

For the above example, Source code is available here. Try out customizing and running it. Happy learning !!

From Spring Boot 2.3.0 buildpack and layered jars support is directly available for both Maven and Gradle. This means we can just type a single command and quickly create Docker images from buildpacks. We would also explore see this feature in my upcoming blog !! Stay tuned !

--

--

Praveen G. Nair

I am a Software Developer and a Technologist. Interested in all cool stuffs of software development, Machine Learning and Cloud. https://praveeng-nair.web.app/