In this article, we are going to learn how to dockerize your Java Springboot project using Jib and Gradle plugin. Jib is a Google open-source Java containerizer that allows Java developers to construct containers using their existing build tools. Jib is a quick and easy container image builder that handles all of the procedures involved in packaging your application as a container image. It does not need the creation of a Dockerfile or the installation of Docker, and it is immediately integrated into Maven or Gradle. So in this article we are going, to use Gradle plugin to dockerize our Springboot application. Let's get started.
Initialize Project
Before you begin this guide you need to have a Springboot project. If you don't have a Springboot project you can create a new Springboot project using Spring Initializer. In this demo I'll use Java Springboot project in my GitHub repository.
Configure Jib
First we need to add Jib to our project. To do that we need to add the following code to our build.gradle file's plugin section.
1 2 3 4
plugins { ... id 'com.google.cloud.tools.jib' version '3.3.2' }
Then we need to add the following code to our build.gradle file’s jib section.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
jib { from { image = "gcr.io/distroless/java@sha256:629d4fdc17eec821242d45497abcb88cc0442c47fd5748baa79d88dde7da3e2d" } to { image = "sampleweb" }
// extra configuration container { jvmFlags = ["-Xms512m", "-Xmx512m"] // optional mainClass = 'mypackage.MyApp'// optional ports = ["8080"] // optional } } // for more configuration options, see https://github.com/GoogleContainerTools/jib/tree/master/jib-gradle-plugin
Since project using Java 11, I have used base image as gcr.io/distroless/java@sha256:629d4fdc17eec821242d45497abcb88cc0442c47fd5748baa79d88dde7da3e2d. If you are using Java 8 you can use gcr.io/distroless/java:8 as base image. The jib.to.image is mandatory. You could set it to project.name or you can pick an unique name to distinguish it from other images. Now we have added Jib and configure it to our project. Let’s build our project and see what happens.
Build with daemon
To build our project with Docker daemon we need to run following command.
1
./gradlew jibDockerBuild
This command will build our project and create a docker image with Docker daemon. Now you can see that our docker image is created. You can check it using following command.
1
docker images
Lets’s assume that we haven’t installed Docker daemon in our machine. In that case we can crate a docker image without Docker daemon and push it to a docker repository. In next section we are going to see how to do that.
Build without daemon
To build our project without Docker daemon and push it to a docker repository we need to add few more lines to our build.gradle file's jib section. .
jib { from { image = 'gcr.io/distroless/java@sha256:629d4fdc17eec821242d45497abcb88cc0442c47fd5748baa79d88dde7da3e2d' } to { image = 'docker.io/<repository>/sampleweb' auth { username = "<username>" password = "<password>" } // This is not the best way to do this. You can use credHelper to do this. }
// extra configuration container { jvmFlags = ["-Xms512m", "-Xmx512m"] // optional mainClass = 'mypackage.MyApp'// optional ports = ["8080"] // optional } } // for more configuration options, see https://github.com/GoogleContainerTools/jib/tree/master/jib-gradle-plugin
Now we can build our project without Docker daemon and push it to a docker repository using following command.
1
./gradlew jib
Conclusion
In this article, we learned how to dockerize your Java Springbot project using Jib and Gradle plugin. You can find the all the related commands for this tutorial from here. If you have any issue regarding this tutorial, mention your issue in the comment section or reach me through my E-mail. In my next article I'll show you how to create complete CI/CD pipeline using GitHub Action to build and publish Docker image. Stay tuned.