Containers are used deploy applications in an isolated environment, once we generate the container images can be deployed number of machines, but for a Java application building an image is not an easy way, we need to write a docker file, run a docker daemon as root, wait for build completion and finally need to push the image to repository.
Google developed jib, an open source library, using this building and pushing image to registry in an easy way.
Jib provides maven and Gradle plugin using which we can generate the container images easily, as it takes cares all packing steps like generating Dockerfile, creating docker daemon to create a container image and push to Docker Registry based on configuration. If the project need other dependencies which are not part project class-path, then we can specify the files/folder to Jib maven plugin to include it in container image.
The typical Docker Build System:
Jib Build System:
Advantages of using Google Jib library:
- Simple: It is written in Java and it run as a part of Maven and Gradle plugin. For traditional way we need to run docker daemon as root user, we need to provide Dockerfile and create flat jar (application jar and its dependencies). Jib is integrating with the application it has the all the required information need to build image.Jib is one of the Maven/Gradle plugin, we can automate the Container Image generation using Jenkins (Continuous Integration Tool).
- Fast: like Docker it takes the advantage of image layering and registry caching to achieve fast, incremental builds. It reads all application configuration and organizes the application into different number of layers and rebuilds the layer if any changes are there. When we execute the Jib tasks it will watch for changes after last build, it only builds necessary changes, and build layer which are related to changes, instead of pushing all layers it will only the push modified layers.
- Reproducible – Jib plugin is built from Maven/ Gradle build files (pom.xml and build.gradle) files to create container images, if inputs are the same to jib it will create reproducible container image.
Integrating Jib to our projects:
Current we are going to learn for Maven build system, similar steps involved for Gradle system also.
Adding Jib plugin in pom.xml file:
See jib-maven-plugin’s Extended Usage tables for more options.
By default, Jib plugin will connect to Docker public Registry for pulling and pushing the images. we need to provide the credential of it in auth section.
Present blog we will learn how to generate and push the spring boot 2 application using jib plugin.
We can create the spring boot 2 application by navigating https://start.spring.io or we can also maven architypes to generate the application.
Add the job maven plugin as specified above.
We can specify the docker hub credentials in pom.xml file or we can use docker credentials helper as given in the below link.
Building Spring Boot Artifact:
We can use mvn clean package to generate the output files for java application and Java software development company.
The above step is independent of Jib library.
Creating Docker Context:
We can use mvn jib:exportDockerContext command to generate the docker context.
If we navigate output directory we can observe jib-docker-context folder is created and it will contain all required files and Dockerfile used to build the Docker image.
Note: The content of target/jib-docker-context could be used to build a Docker image using docker build -t sravan4rmhyd/springboot2-docker-demo:1.0.1 target/jib-docker-context command.
Docker file looks likes as below:
Jib plugin is also used create docker daemon to push the images to Docker registry.
Setting up the Docker Daemon:
We can add the below server in .m2/settings,xml file
Where username and password are the docker hub credentials, and its recommend using encrypted password as specified in the below link.
once the it is done, we can invoke mvn jib:build command to push the image to repository.
We can see the uploaded image in docker hub registry.
We can download the source code of the application in below url:
Google recently announced Jib, an open-source container image builder that lets #Java developers build Java containers using well-known Java tools such as #Maven or #Gradle. Find out how to use it. https://t.co/aywSnfTtwg— InfoQ (@InfoQ) September 1, 2018
Google jib is the open source library which is used to build docker images and push to registry without need of Docker daemon, it reduces all complexities in writing Dockerfile and build procedures.