Ad Code

Ticker

6/recent/ticker-posts

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

 

In this Example we are going to create Docker image without installing a Docker client or using Dockerfile for our SpringBoot application. Here we are going use Jib Maven Plugin for building Docker image of Spring Boot Application.   

Jib handles all steps of packaging your application into a container image. You don't need to know best practices for creating Dockerfiles or have Docker installed. It is a Java containerizer from Google that lets Java developers build containers using build tools like Maven, Gradle, etc.

Below Snipped shown us the difference between Docker build flow vs Jib build flow.


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 maven project using SpringBoot and jib maven plugin in the pom.xml

Here we have configured       
  • The jib-maven-plugin from </groupId> com.google.cloud.tools.
  • Under </configuration> we have provided the docker hub registry and image name.
  • Under </auth> we have provided the credential for docker hub registry.
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<to>
<image>registry.hub.docker.com/rajivksingh13/my-teachlea:1.0.0</image>
<auth>
<username>********</username>
<password>********</password>
</auth>
</to>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>

2. Create a /hello Rest End Point.

@GetMapping("/hello")
public String getMessage(){
return "Hello From Docker Image Created using Maven 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 mvn package.

Build your container image within Intellij Idea:



Build your container image with command:
mvn compile jib:build

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.

mvn compile jib:dockerBuild

Build an image tarball:
You can build and save your image to disk as a tarball with:

mvn compile jib:buildTar

This builds and saves your image to target/jib-image.tar, which you can load into docker with:

docker load --input target/jib-image.tar

Bind to a lifecycle:
You can also bind jib:build to a Maven lifecycle, such as package, by adding the following execution to your jib-maven-plugin definition:

<plugin>
  <groupId>com.google.cloud.tools</groupId>
  <artifactId>jib-maven-plugin</artifactId>
  ...
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>build</goal>
      </goals>
    </execution>
  </executions>
</plugin>
Then, you can build your container image by running:

  mvn package






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, 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.

docker run -i --rm -p 8080:8080 rajivksingh13/my-teachlea:1.0.0



After Successful Pull our Spring Boot application will start run as a docker container as show below: 



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 Maven Plugin. We are going to check in our next post 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