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.

docker build process

And Jib is built like this.

jib build process

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>jib-maven-plugin</artifactId>
    <version>2.4.0</version>
    <configuration>
        <!-- 相当于 Dockerfile 中的 FROM -->
        <from>
            <image>amazoncorretto:8</image>
        </from>
        <to>
            <!--构建镜像名称,这里我使用maven中定义的项目名称-->
            <image>daxus/${project.name}</image>
            <!--私有仓库的账号密码-->
            <auth>
                <username>felordcn</username>
                <password>yourpassword</password>
            </auth>
            <!--Docker 镜像的 tag 这里使用maven定义的版本号-->
            <tags>
                <tag>
                    ${project.version}
                </tag>
            </tags>
        </to>
    </configuration>
</plugin>

Then execute mvn clean compile jib:build in the root of the project and you’re done.

jib build

It is also possible to simply introduce the Jib plug-in.

1
2
3
4
5
<plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>jib-maven-plugin</artifactId>
    <version>2.4.0</version>
</plugin>

Only our command will be a bit more complex and will require specifying some necessary parameters, e.g.

1
2
3
4
mvn clean compile jib:build \
    -Djib.to.image=myregistry/myimage:latest \
    -Djib.to.auth.username=$USERNAME \
    -Djib.to.auth.password=$PASSWORD

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.