1. Preface
Jib is a class library developed by Google to build Docker and OCI images of Java applications directly, provided as Maven and Gradle plugins. The best part is that it can be built without the Docker daemon, i.e. you don’t have to install the docker daemon on your computer! Although Spring Boot 2.3.0.RELEASE has already been released with the ability to build images, I couldn’t resist trying Jib.
2. Docker build process and Jib’s build process
Contrast the build process of these two.
The Docker build process requires us to first type the project into Jar and then write a Dockerfile and then use the Docker build feature to build the image and run the container. The process is as follows.
And Jib is built like this.
As a Java developer, instead of caring about all kinds of extraneous commands and operations, you only need to focus on Java, and efficient and stable as well as reusable incremental builds. Why is Jib so fast and efficient?
Traditionally, Java applications are built as a single image layer along with the application Jar, whereas Jib’s build strategy divides Java applications into multiple layers for more granular incremental builds. When changing code, only the changes are rebuilt, not the entire application.
3. Jib Building Spring Boot Applications
Next I will show how to image the Spring Boot application and upload it to the Dockerhub repository.
Using the Maven project as an example, we just need to introduce the Jib Maven plugin in pom.xml
. By default Jib will upload our image to Google’s gcr.io repository, in practice we will upload our image to a private repository, so we need to add some personalized configuration. Here I will use dockerhub repository as an example to add some personalized configuration.
|
|
Then execute mvn clean compile jib:build
in the root of the project and you’re done.
It is also possible to simply introduce the Jib plug-in.
Only our command will be a bit more complex and will require specifying some necessary parameters, e.g.
For more custom commands, please refer to the official documentation:
https://github.com/GoogleContainerTools/jib/tree/master/jib-maven-plugin#extended-usage
4. Summary
Jib is very easy to use and allows developers to build Docker images in the style of Java, which can greatly improve the programming experience.