Packaging and shipping software as containers has numerous benefits, but doing it without the right tools and techniques leads to poor developer experience.
Some of the challenges that i faced are:
Local Installation of Docker : Getting Docker installed with root account or account with root privileges has been a challenge due to organizations security policies and this had to go through security reviews and approvals.
DockerFile Best practices: Knowledge of best practices to write Docker File helps in generating containers that are as ephemeral as possible. But how do we ensure team is following these best practices ???
Image Size: In scenarios where image size is of concern, how do we ensure that the image size is optimal and base images are optimized for example openjdk is 284 MB and alpine jdk is 82 MB. so how do we ensure an optimal base is image is used by teams to minimize the image size.
Build Time: Even if we change a small line in the project directory or context, docker runs the entire build and this increases the build time. For example i have a simple hello world application, the first build took around 2 minutes, second time it took 17 seconds as the image was cached and when i changed a word in the description of pom.xml docker ran the entire build and it took 79.7 seconds again
Over all the process to build and upload the container to image registry is
If the overhead of writing Docker File, having Docker Daemon or CLI on local , picking right base images can be eliminated and if the over all process can be made as simple as depicted in the below image, the developer experience can be significantly improved
Jib is an open source Java containerizer project from Google, that builds optimized Docker and OCI images for your java applications without a Docker daemon and writing Docker Files
Jib has following features
Fast : Jib separates your application into multiple layers, splitting dependencies from classes. Now you don’t have to wait for Docker to rebuild your entire Java application – just deploy the layers that changed.
Daemonless: Jib doesn’t require docker CLI and builds your image using maven / gradle and pushes to the registry
Can build an image without Dockerfiles
Reproducible: Rebuilding your container image with the same contents always generates the same image. Good read on reproducibility
The layers created by Jib are created by default on top of a distroless base image. These 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.
We can always overwrite and specify a base image of our choice like openjdk or alpine jdk but distroless images provide better security and are built with minimum dependencies.
Let us see a demo now, i have a simple spring boot application in my git repo.
This application does not have any docker file and to use Jib, i will have to update the pom.xml file to use Jib plugin
In my example i have just added the configuration to upload my image to my docker repo, when i use the mvn package goal.
The base java image will be distroless image, but i can also configure it using from tag . In this example the image will be uploaded to docker hub but in the configuration section we can mention any repo like JFrog, AWS ECR.
Details of the various configuration tags can be found at this location. There are various ways to configure the repository credentials as well but in this example i have used docker login on my console already.
Now if i do mvn package , maven will compile the application, build the docker image and upload it to the registry with one single command and without the need of DockerFile
Image uploaded to docker repository
Jib simplifies the process of containerizing java based applications and accelerates the development process and enhances developer experience. It can also very well get integrated into the existing build or CI/CD systems
Thank you and please do share your comments and thoughts on this subject
Thanks Rajesh for sharing this article. Jib looks powerful and the way you explained it just amazing.
Thank you Naga !!