The advent of containers has brought Java developers closer than ever to a “write once, run anywhere” workflow, but containerizing Java applications is no easy task: You have to write a Dockerfile, run the Docker daemon as root, wait for the build to complete, and finally push the image to a remote registry. But not all Java developers are container experts. Are the days of just building a JAR package over?

To address this challenge, Google has opened source a Java container chemical called Jib, which allows Java developers to build containers using Java tools they are familiar with. Jib is a quick and simple container image builder that takes care of all the steps required to package applications into container images. It doesn’t require you to write dockerfiles or install Docker, and it integrates directly into Maven and Gradle — just add plug-ins to your build and you can immediately containerize Java applications.

Docker build process:

Jib construction process:

How does Jib make development better

Jib makes use of the layered mechanism of Docker image, integrates it with the construction system, and optimizes the construction of Java container image in the following ways:

  1. Simple – Jib is developed in Java and runs as part of Maven or Gradle. You don’t need to write a Dockerfile or run a Docker daemon, or even create a big JAR package with all your dependencies. Because the Jib is tightly integrated with the Java build process, it has access to all the information needed to package the application. During subsequent container builds, it will automatically select any variants Java has built.
  2. Fast – The Jib uses image layering and registry caching for fast, incremental builds. It reads your build configuration, organizes your application into different layers (dependencies, resources, classes), and only rebuilds and pushes the layers that have changed. During rapid iterations of the project, the Jib pushes only the layers that have changed (not the entire application) to the registry to save valuable build time.
  3. Reproducible – The Jib supports declarative container image builds based on Maven and Gradle build metadata, so the same image can be created repeatedly through configuration as long as the input remains constant.

How does the Jib container your application

The Jib can be used as a plugin for Maven and Gradle and requires minimal configuration. Simply add the plug-in to the build definition and configure the target image. If you want to push the image to a private registry, configure the required secret keys for the Jib. The simplest way is to use a credential helper like Docker-credential-gCR. The Jib also provides additional rules for building images into the Docker daemon.

Using the Jib in Maven:

__Fri Jul 13 2018 15:33:01 GMT+0800 (CST)____Fri Jul 13 2018 15:33:01 GMT+0800 (CST)__<plugin> < the groupId > com. Google. Cloud. Tools < / groupId > < artifactId > jib - maven - plugin < / artifactId > < version > 0.9.0 < / version > <configuration> <to> <image>gcr.io/my-project/image-built-with-jib</image> </to> </configuration> </plugin>__Fri Jul 13 2018 15:33:01 GMT+0800 (CST)____Fri Jul 13 2018 15:33:01 GMT+0800 (CST)__Copy the code

 

__Fri Jul 13 2018 15:33:01 GMT+0800 (CST)____Fri Jul 13 2018 15:33:01 GMT+0800 (CST)__# Builds to a container image registry.
$ mvn compile jib:build
# Builds to a Docker daemon.
$ mvn compile jib:dockerBuild
__Fri Jul 13 2018 15:33:01 GMT+0800 (CST)____Fri Jul 13 2018 15:33:01 GMT+0800 (CST)__Copy the code

Use Jib in Gradle:

__Fri Jul 13 2018 15:33:01 GMT+0800 (CST)____Fri Jul 13 2018 15:33:01 GMT+0800 (CST)__plugins { id 'com. Google. Cloud. Tools. Jib' version '0.9.0} jib. To. Image =' GCR. IO/my - project/image - built - with - jib '__Fri Jul 13, 2018  15:33:01 GMT+0800 (CST)____Fri Jul 13 2018 15:33:01 GMT+0800 (CST)__Copy the code

 

__Fri Jul 13 2018 15:33:01 GMT+0800 (CST)____Fri Jul 13 2018 15:33:01 GMT+0800 (CST)__# Builds to a container image registry.
$ gradle jib
# Builds to a Docker daemon.
$ gradle jibDockerBuild
__Fri Jul 13 2018 15:33:01 GMT+0800 (CST)____Fri Jul 13 2018 15:33:01 GMT+0800 (CST)__Copy the code

Jib project address: github.com/GoogleConta… .