Containerizing SpringBoot Application With Jib
How to create Docker or OCI complaint images without installing any Docker clients or using Dockerfiles for a Spring Boot application by using Google's Jib.
Join the DZone community and get the full member experience.Join For Free
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.
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.).
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
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.
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.
Using the Command Line
Just run the below command to build an image of your application. Make sure you have Maven installed.
This will compile, build, and then push your application's image to the configured container registry.
The following is the output:
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.
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.
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.
You can also read some of my previous articles here.
Published at DZone with permission of Ashish Choudhary. See the original article here.
Opinions expressed by DZone contributors are their own.