Jib was released by Google on July 11, 2018. Jib is an OSS that automatically turns Java applications into Docker containers. It is published as a plugin for Maven and Gradle, and you can easily use it by simply registering Jib as a plugin. Also, you don't have to write a Dockerfile yourself at all, and Jib will automatically create the image for you. Until now, I wrote Dockerfile and docker-compose.yml to build the image, but it is very easy because the image is built automatically just by building with mvn or gradle. In this article, I will introduce you to create a Spring application (Hello world) with Maven project and see the application from docker.
First, create a Spring template with SPRING INITIALIZR.
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.jib</groupId>
<artifactId>jibDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>jibDemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
</plugins>
</build>
</project>
Next, add the Jib settings.
<!-- Jib -->
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>0.9.6</version>
<configuration>
<to>
<image>hellojib</image>
</to>
</configuration>
</plugin>
Add the Jib plugin to the build part.
After that, just import with eclipse or Intellij.
On the application side, only Hello World this time
HelloController
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/")
public String sayHello(Model model) {
model.addAttribute("message", "Hello World!!!");
return "index";
}
}
index.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Hello Spring</title>
</head>
<body>
<span th:text="${message}">Hello world</span>
</body>
</html>
Docker Image Build Create a Docker image in Jib
$ mvn compile jib:build
If successful, a Docker image will be generated.
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hellojib latest 16ab4943a4d8 48 years ago 138MB
The Docker image name can be set in the configuration in pom.xml. This time, hellojib is set for image.
<configuration>
<to>
<image>hellojib</image>
</to>
</configuration>
After that, you can start Java application with Docker like docker run --name hellojib -d -p 8080: 8080 hellojibD
.
Recommended Posts