1. Introduction
In this tutorial, we’ll generate a Docker image of Spring Boot application with Google Jib.
2. What is Google Jib?
Google Jib is an open-source Java containerization tool that allows developers to build Docker and OCI (Open Container Initiative) images for Java applications without needing a Docker daemon or writing a Dockerfile. Jib integrates directly with popular Java build tools like Maven and Gradle, simplifying the process of creating container images.
2.1 Key feature of Google Jib
- No Docker Required: You don’t need Docker installed to use Jib. It builds images directly using Java build tools.
- Faster Builds: Jib optimizes the build process by layering application files intelligently, which helps reduce image size and speeds up subsequent builds.
- Integrated with Maven and Gradle: Easily configure Jib within your
pom.xml
(Maven) orbuild.gradle
(Gradle) files. - Customizable: You can specify the base image, JVM flags, and entry points for the application.
- Push to Registries: Jib supports pushing images directly to container registries like Docker Hub, Google Container Registry, and others.
3. jib-maven-plugin
In this tutorial, we’ll use the jib-maven-plugin
to create Docker image.
To setup Jib, add the following plugin in your pom.xml
.
<plugin> <groupId>com.google.cloud.tools</groupId> <artifactId>jib-maven-plugin</artifactId> <version>3.3.2</version> <configuration> <to> <image>demoimage</image> </to> </configuration> </plugin>
Following is the complete pom.xml
:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.3.5</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.learnitweb</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <url /> <licenses> <license /> </licenses> <developers> <developer /> </developers> <scm> <connection /> <developerConnection /> <tag /> <url /> </scm> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.google.cloud.tools</groupId> <artifactId>jib-maven-plugin</artifactId> <version>3.3.2</version> <configuration> <to> <image>demoimage</image> </to> </configuration> </plugin> </plugins> </build> </project>
We’ll create a HelloController
to test our application:
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") public class HelloController { @GetMapping("/hello") public String sayHello() { return "Hello, World!"; } }
4. Building image
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
5. Run the Docker image
Once the image is ready, you can run the image.
docker run -p 8081:8080 demoimage