Demystifying Google Container Tool Jib: Java Image Builder
Tags: java, maven, container
Published: 2021-01-21 04:21:36

> This article covers some internals of image layering created by container image builder Jib and explore what distroless images are and their benefits.

Are you are wondering what Jib is, actually?. You probably should read my previous [article](https://ashishtechmill.com/containerizing-spring-boot-application-with-jib) before going ahead.


Jib is an excellent tool for Java developers who are interested in containerizing Java applications but not so interested in creating and maintaining Dockerfile or installing Docker. 
A Java developer can add a plugin to Maven or Gradle, and that's it. You don't have to learn new technology just to containerize your Java application.

Now let's talk about some internals.

### Jib Image Layering
Jib intelligently divides your images into the following layers for more granular incremental builds.

1. Dependencies
1. Resources
1. Classes
1. Snapshot dependencies
1. All other dependencies
Each extra directory (jib.extraDirectories in Gradle, <extraDirectories> in Maven) builds to its own layer

When you do any code changes, only your changes are rebuilt, not your entire application. Which means Jib only pushes the layer which is changed and the rest of the layers remain the same. 
For example, in the following demo, only a class file was changed and Jib pushed only the compiled class layer. Later on, we could see that during the docker pull only the changed layer was extracted.

{% youtube vizfV2opkfU %}

Furthermore to dig deeper, now we will use the simple spring boot application that was used in the previous [article](https://ashishtechmill.com/containerizing-spring-boot-application-with-jib?guid=1501e406-2971-4eb0-860a-c6efbd39dd33&deviceId=3c87702f-1483-47b1-a280-58efcd89db75). The source code is available [here](https://github.com/yrashish/spring-boot-jib). We will run maven in debug mode to understand these layers. However, In the following logs, you will see only dependencies, resources, and classes as separate layers since we don't have other dependencies.

{% gist https://gist.github.com/yrashish/22251e28cb2c600c9b1978ac22439580 %}

### Deep Dive Image Layer
You can inspect images created by Jib using the [Dive](https://github.com/wagoodman/dive) tool. As you can see on RHS, we have exploded directory structure for classes, resources and dependencies in the following path and their size is very minimal.

`/apps/classes,/apps/libs,/apps/resources`

Rest of the contents are of your base image.

![Screenshot 2021-01-08 at 7.30.23 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1611246809879/6BWAJwpEI.png)

Dive also tells you if you are wasting any space and if you are then you can discover ways to shrink the size of your Docker/OCI image. However, in our case, no space is wasted :).!
You can view the content of an image with a docker history command as well. Let's see what it looks like.

![Screenshot 2021-01-09 at 1.15.58 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1611168829601/xK66ceNKF.png)

### What are Distroless Images?
Google defines distroless images as the following:

> Distroless images are lightweight base images as they contain only dependencies required to run your application. They do not contain package managers, shells or any other programs you would expect to find in a standard Linux distribution.

Furthermore, the layers created by Jib are created on top of a distroless base image. Jib by default uses distroless Java 8 image but you have the option to choose an image of your choice.

### Why Distroless?
1. Fewer vulnerabilities to patch
1. Better security
1. Built with minimum dependencies


### Debugging Distroless Images
Since there are no package managers installed and you cannot do ssh to your container running with distroless base image makes them hard for debugging. In an ideal world, you would add better logging then you know enable shell access for your containers. But there are ways with which you can add shell support and [debug](https://github.com/GoogleContainerTools/distroless#debug-images) your application.


### Conclusion
In this article, we have covered some internals about Jib .i.e. image layering which makes it fast. We have also covered distroless images with their benefits and pitfalls like debugging.

### Support me

If you like what you just read then you can buy me a coffee
<a href="https://www.buymeacoffee.com/meashish" target="_blank" rel="noopener"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important"></a>

**Further reading**

You can also read some of my previous [articles](https://ashishtechmill.com/)