Containerizing SpringBoot Application With Jib

In this article, we will learn how to create Docker or OCI complaint images without installing a Docker client or using Dockerfile for our SpringBoot application. We will be doing all of this with the help of Jib.

What Is Jib?

Jib is a Java containerizer from Google that lets Java developers build containers using build tools like Maven, Gradle, etc.

Jib
Jib (Image source)

But that’s not all that is really interesting about Jib, because you don’t need to know anything about installing Docker, maintaining Dockerfiles, etc. As a developer, you only care about the artifact (jar, war, etc.) that you will produce and you don’t have to deal with any of the Docker nonsense (build/push, etc.).

Jib v/s Docker build flow(Image source Google Cloud)
Jib vs. Docker build flow (Image source Google Cloud)

Wow, this is really powerful! But how?

How to Jib

With Jib, you can containerize your Java applications in no time by adding a Maven or Gradle plugin to your pom.xml.or build.gradle file. It is that simple. We will be covering Maven first and in the later post we'll touch on Gradle. Let’s get started then.

We will be using Spring Initializr to generate a working Spring Boot project. The source code for our Spring Boot application is available here. It just prints a Hello message when the image is pushed via Jib and the image is run through Docker.

Once we are set up ready with an IDE, we can proceed with the next step.

Setting Up Maven

XML
 






For Maven, you can paste the above content into your pom.xml plugin section and you are good to go. But I will try to explain the <from> and <image> tags here.

<from> configures the base image on which to build your application.

Typically, you don’t need to provide <from> as by default, as it uses a distro-less Java 8 image. However, I have used Java 11, so I have explicitly mentioned that here. Moreover, depending on your use case, you may want to use a different base image.

<image> This refers to the target image that will pushed to the container registry.

I have used a Docker registry, but you can use any cloud provider's (ECS, GCR, ACR) container registry.

For other options available for use with the plugin, you can refer to the documentation.

Setting Credentials for Registry

To push an image, we will need to add registry credentials to the Maven settings.xml file. Since we are just doing a demo, it’s okay to provide credentials this way, but avoid using it in real-world projects as it is not secure. You may want to secure credentials as mentioned here.

XML
 






Building an Image

To build an image, we can do it in the following ways.

With an IDE

For example, in IntelliJ, you can go to the Maven view of your project, then go under Plugins>jib, then right-click and run the Maven build. You may want to create an IntelliJ run configuration that can run Maven goals like clean, compile, etc., then push your image.

run maven build

Using the Command Line

Just run the below command to build an image of your application. Make sure you have Maven installed.

Shell
 




xxxxxxxxxx
1


 
1
mvn compile jib:build


This will compile, build, and then push your application's image to the configured container registry.

The following is the output:

Plain Text
 




xxxxxxxxxx
1
87


 
1
ashish@MacBook-Air springboot % mvn compile jib:build
2
[INFO] Scanning for projects…
3
[INFO]
4
[INFO]           < com.example:spring-boot-jib >           -
5
[INFO] Building springboot 0.0.1-SNAPSHOT
6
[INFO]                 [ jar ]                 -
7
[INFO]
8
[INFO]   maven-resources-plugin:3.1.0:resources (default-resources) @ spring-boot-jib  -
9
[INFO] Using ‘UTF-8 encoding to copy filtered resources.
10
[INFO] Copying 1 resource
11
[INFO] Copying 0 resource
12
[INFO]
13
[INFO]   maven-compiler-plugin:3.8.1:compile (default-compile) @ spring-boot-jib  -
14
[INFO] Nothing to compile  all classes are up to date
15
[INFO]
16
[INFO]   jib-maven-plugin:2.6.0:build (default-cli) @ spring-boot-jib  -
17
[WARNING] ‘mainClass’ configured in ‘maven-jar-plugin’ is not a valid Java class: ${start-class}
18
[INFO]
19
[INFO] Containerizing application to registry.hub.docker.com/hiashish/spring-boot-jib-image…
20
[WARNING] Base image ‘gcr.io/distroless/java:11 does not use a specific image digest  build may not be reproducible
21
[INFO] Using credentials from Maven settings file for registry.hub.docker.com/hiashish/spring-boot-jib-image
22
[INFO] Using base image with digest: sha256:b25c7a4f771209c2899b6c8a24fda89612b5e55200ab14aa10428f60fd5ef1d1
23
[INFO]
24
[INFO] Executing tasks:
25
[INFO]
26
[INFO] Executing tasks:
27
[INFO]
28
[INFO] Executing tasks:
29
[INFO] [======= ] 25.0% complete
30
[INFO]
31
[INFO] Executing tasks:
32
[INFO] [======= ] 25.0% complete
33
[INFO] > pushing blob sha256:6508f436f385b3751366f90b6…
34
[INFO]
35
[INFO] Executing tasks:
36
[INFO] [======= ] 25.0% complete
37
[INFO] > pushing blob sha256:6508f436f385b3751366f90b6…
38
[INFO] > pushing blob sha256:c5e22041fc97b838b93a2e18d…
39
[INFO]
40
[INFO] Executing tasks:
41
[INFO] [======= ] 25.0% complete
42
[INFO] > pushing blob sha256:6508f436f385b3751366f90b6…
43
[INFO] > pushing blob sha256:c5e22041fc97b838b93a2e18d…
44
[INFO] > pushing blob sha256:b25902383f9ee26808b68ca62…
45
[INFO]
46
[INFO] Executing tasks:
47
[INFO] [======= ] 25.0% complete
48
[INFO] > pushing blob sha256:6508f436f385b3751366f90b6…
49
[INFO] > pushing blob sha256:c5e22041fc97b838b93a2e18d…
50
[INFO] > pushing blob sha256:b25902383f9ee26808b68ca62…
51
[INFO] > checking base image layer sha256:31eb28996804…
52
[INFO]
53
[INFO] Executing tasks:
54
[INFO] [======== ] 27.8% complete
55
[INFO] > pushing blob sha256:c5e22041fc97b838b93a2e18d…
56
[INFO] > pushing blob sha256:b25902383f9ee26808b68ca62…
57
[INFO] > checking base image layer sha256:31eb28996804…
58
[INFO]
59
[INFO] Executing tasks:
60
[INFO] [========= ] 30.6% complete
61
[INFO] > pushing blob sha256:c5e22041fc97b838b93a2e18d…
62
[INFO] > checking base image layer sha256:31eb28996804…
63
[INFO]
64
[INFO] Executing tasks:
65
[INFO] [========== ] 33.3% complete
66
[INFO] > checking base image layer sha256:31eb28996804…
67
[INFO]
68
[INFO] Executing tasks:
69
[INFO] [=========== ] 35.0% complete
70
[INFO]
71
[INFO] Executing tasks:
72
[INFO]
73
[INFO]
74
[INFO]
75
[INFO] Container entrypoint set to [java, -cp, /app/resources:/app/classes:/app/libs/*, com.jib.example.spring.SpringbootApplication]
76
[INFO]
77
[INFO] Built and pushed image as registry.hub.docker.com/hiashish/spring-boot-jib-image
78
[INFO] Executing tasks:
79
[INFO] [=========================== ] 91.7% complete
80
[INFO] > launching layer pushers
81
[INFO]
82
[INFO] — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
83
[INFO] BUILD SUCCESS
84
[INFO] — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
85
[INFO] Total time: 8.746 s
86
[INFO] Finished at: 2020–11–16T02:34:33+05:30
87
[INFO] — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —


Running an Image

We have successfully pushed the image (image name: spring-boot-jib-image) to a Docker registry. Now we can run the image using Docker.

running an image
Run the image

As you can see, our application is running inside a container. Now just run the curl command and you will get a hello message from our Spring Boot application.

hello message

Hello message

Conclusion

In this article, we have learned how we can containerize our Java applications without Docker. Additionally, with Jib, you can build images using Docker, but that’s not the X factor. Other benefits of using Jib for your Java applications include super easy integrating with Java applications, faster builds, reproducible builds, it's backed by Google, and more. You can go to this link to learn more about Jib's benefits in detail.

Further Reading 

You can also read some of my previous articles here.

 

 

 

 

Top