Dockerizing Java Applications with Jib

Ali Emrah Pekesen
KoçSistem
Published in
3 min readMay 29, 2020

Introduction

Briefly, Jib is a container tool that builds, pushes the Docker images of Java applications without a Docker daemon. Means, you don’t have to have a docker daemon installed on your machine!

From a developer’s perspective, my favorite sentence from its official documentation is “No more writing Dockerfiles and calling docker build/push”.

From a DevOps engineer perspective, most probably the favorite sentence is “Jib’s build strategy separates the Java application into multiple layers for more granular incremental builds. When you change your code, only your changes are rebuilt, not your entire application. These layers, by default, are layered on top of a distroless base image.

Take a deeper look at Jib Github Repository.

The differences between standard Docker Build Flow and Jib Build Flow

Docker build flow

Jib build flow

As you see, Jib handles the image build/push responsibilities on itself. No more Dockerfile generation nor docker-cli command execution needed. In the scope of this article, we are going to focus on handling the docker image build step with Jib.

We use the Jib maven plugin to ease the managing docker images during maven phase execution by defining the particular goal. Let’s add the Jib maven plugin into your pom.xml file and fill the required values as below.

With the above configuration, our docker image is going to be generated during the maven packaging phase and once we run up a new container from the newly created image (tagged as jib-generated_1.0), the JVM is going to be started with the given arguments itself.

First, run the below maven command to package our maven project then check the docker image list.

mvn clean package#for linux
docker image ls |grep "jib-usage"
#for windows
docker image ls |findstr "jib-usage"

You see the image that is tagged as jib-generated_1.0 has already been built and pushed to the local image repository.

Let’s run a new container from the jib-usage image and see whether it works properly.

docker run -d -p 8080:8080 jib-usage:jib-generated_1.0

See in below, our container is up and running properly.

Conclusion

At the end of this article, you are able to use Jib to handle docker image build/push steps and run your java application that runs on a docker container. I hope it helps and makes your daily routines easier. If you have any questions, do not hesitate to contact me.

--

--