Ad Code

Ticker

6/recent/ticker-posts

Containerizing Spring Boot Application with Gradle Jib- Create Docker Image and Publish to Docker hub using Jib Gradle Plugin

 In our previous example we have seen how to create Docker Image and Publish to Docker hub registry using Jib Maven Plugin for a Spring Boot Application. In this example we are going to see how we can achieve the same using Jib Gradle Plugin.

Prerequisites To complete this example:
  • An IDE
  • JDK 11+ installed with JAVA_HOME configured appropriately
  • Apache Maven 3.8.1+
  • Docker Desktop
Here are the Steps:
1. Create a gradle project using SpringBoot and jib gradle plugin in the build.gradle fileHere we have configured       
  • The jib-gradle-plugin from  com.google.cloud.tools.
  • Written a gradle build script for jib where we have provided the docker hub registry, image name and the credential for docker hub registry.
jib {
from {
image = 'openjdk:alpine'
}
to {
image = 'registry.hub.docker.com/rajivksingh13/my-gradle-teachlea:1.0.1'
auth {
username = '********'
password = '********'
}
}
}
2. Create a /hello Rest End Point.
@GetMapping("/hello")
public String getMessage(){
return "Hello From Docker Image Created using Gradle Jib Plugin";
}
3. There are different ways to build the image and I have mention below few of them, 
    But In this Example we are going to use the command from Intellij Idea.
Build your container image within Intellij Idea:
It will Successfully Create Docker Image and Publish to Docker hub registry

Build your container image with:
gradle jib

Build to Docker daemon:
Jib can also build your image directly to a Docker daemon. This uses the docker command line tool and requires that you have docker available on your PATH.
gradle jibDockerBuild

If you are using minikube's remote Docker daemon, make sure you set up the correct environment variables to point to the remote daemon:

eval $(minikube docker-env)
gradle jibDockerBuild
Alternatively, you can set environment variables in the Jib configuration.

Build an image tarball:

You can build and save your image to disk as a tarball with:
gradle jibBuildTar
This builds and saves your image to build/jib-image.tar, which you can load into docker with:
docker load --input build/jib-image.tar

4. After Successful build login to you Docker Hub Registry and you will able to see the newly created image will be created there.


5. Now its time run and test our docker image, for this use the below command
docker run -i --rm -p 8080:8080 rajivksingh13/my-gradle-teachlea:1.0.1
You can see it will first try to find the docker image locally but unable to find out. So Next it will try to pull the image from remote Docker Hub Registry.



6. Now open http://localhost:8080/hello in any browser and test our Rest End Point:


Summary:
So in the Example we have seen and implemented how can we Containerize a Spring Boot Application using Jib Gradle Plugin

GitHub Link : rajivksingh13/teachlea

Please feel free to provide your valuable comments, Thanks.

Post a Comment

0 Comments

Ad Code