Table of contents

Introduction

Nowadays, you hear a lot about the term ‘containerization’ and the benefits of this technology. Containerization has become a very popular thing in software and cloud development as more users and companies start to adopt it. Because of this, user experience and usability also have to grow. In other words, how do we make the creation of a Docker image as easy as possible for a developer? After all, we do love it when things just work.

The process of creating a Docker image has always evolved in the years to be as simple as possible. With the amount of resources and information we have today, it shouldn’t be hard to write your own Dockerfile. You can also use a Maven / Gradle plugin which interacts with your Docker CLI if you want to avoid writing Dockerfiles.

But what if it could be even more simpler?

Jib

Enter Jib. Jib is an image builder plugin released in 2018 and made and maintained by Google. To explain what it does, I will use a quote from the official Google Blog announcement:

Jib is a fast and simple container image builder that handles all the steps of packaging your application into a container image. It does not require you to write a Dockerfile or have docker installed, and it is directly integrated into Maven and Gradle.

This seems like a big thing, doesn’t it? Overall, the biggest benefits of Jib are:

  • No need for a Docker daemon anymore in your environment;
  • No seperate docker build / push commands in your pipeline;
  • Supports both Maven and Gradle projects;
  • Dockerizes your application into multiple layers and separates your dependencies from your own classes to significantly reduce build time (in contrast to building fat JARs);
  • Jib only builds the changed layers, so unless you add / update dependencies, it won’t rebuild the dependency layer!

Usage

Using the Jib plugin is pretty easy and straight-forward as it was designed to be so. All properties and settings work perfectly out of the box. This means that I can just add the plugin to my build tool and it will be able to create a Docker image without further ado. However, if your container setup is more complex, Jib allows you to override every property that best suits your needs. You no longer need a Dockerfile in your project, Jib is perfectly capable of handling your image setup on its own.

You can try the Jib plugin by either using your current Java project or by cloning the Spring Pet Clinic Sample Application.

Once you have your project open in your IDE, open the pom.xml (Maven) or build.gradle (Gradle) file to start adding the Jib plugin. Don’t forget to build a fresh JAR!

Gradle

To use Jib in Gradle, you first need to add the plugin to your build.gradle file. You can do this in the plugins block.

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

Afterwards, you can add your Jib plugin configuration to the file, with the settings that you prefer.

jib {
  to {
    image 'yolanv/jib-demo:gradle'
  }
}

---

jib.to.image = 'yolanv/jib-demo:gradle'

Two ways of specifying your Jib configuration in Gradle

Now you can run the following command:

gradle jib

Maven

With Jib for Maven, the configuration is more or less the same as in Gradle, except that you need to write it in XML.

In Maven, you have to add your preferred configuration with the plugin declaration. Add the following in the plugins element:

<plugin>
  <groupId>com.google.cloud.tools</groupId>
  <artifactId>jib-maven-plugin</artifactId>
  <version>2.1.0</version>
  <configuration>
    <to>
      <image>yolanv/jib-demo:maven</image>
    </to>
  </configuration>
</plugin>

You can trigger the Jib plugin with the following command:

mvn compile jib:build


The plugin configuration tells Jib that, whenever Jib gets triggered, it should build an image named yolanv/jib-demo with the corresponding image tag. Running the command will trigger Jib to start building and pushing your Docker image.

Conclusion

In a short time, we were able to successfully build a Docker image and push it to our registry. It requires very little configuration (depending on your setup ofcourse), it only builds the changed layers and we don’t even need to have a Docker daemon installed!

Docker repository

Docker Hub repository with two pushed tags done with Jib

Personally, I think this is a very powerful plugin that will be used in new and old projects. While I think that it is important for people to understand how Docker works and how Docker images are built, Jib still gives a lot of advantages over using regular Dockerfiles.

Useful links

Yolan Vloeberghs is a Java Consultant with a keen passion for Spring Boot projects and Java technology. He loves to play around with various technologies and frameworks and is very passionated and eager to learn about everything related to cloud development.