Easily Docker Java applications with Jib

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.

Maven project creation

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.

Java application

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

Easily Docker Java applications with Jib
Install java with Ubuntu 16.04 based Docker
Monitor Java applications with jolokia and hawtio
CICS-Run Java applications-(3) Build management with Gradle
CICS-Run Java applications-(2) Build management with Maven
Create Java applications with IBM Cloud Functions
Easily convert Java application to Docker container with Jib ~ Build with gradle and register in local repository
/ n \ n docker java
[Memo] Create a CentOS 8 environment easily with Docker
Make SpringBoot1.5 + Gradle4.4 + Java8 + Docker environment compatible with Java11
Easily switch Java versions with alias on macOS
Build and test Java + Gradle applications with Wercker
Prepare a scraping environment with Docker and Java
Docker Container Operations with Docker-Client API for Java
CI the architecture of Java / Kotlin applications with ArchUnit
Change seats with java
Install Java with Ansible
Launch MariaDB with Docker
Rails deploy with Docker
Run Pico with docker
Easily build a Vue.js environment with Docker + Vue CLI
Explode Docker with WSL2
Use Puphpeteer with Docker
Comfortable download with JAVA
Switch java with direnv
Operate Emby with Docker
Try WildFly with Docker
Use ngrok with Docker
Run Payara with Docker
[Note] Create a java environment from scratch with docker
Download Java with Ansible
Try connecting to AzureCosmosDB Emulator for Docker with Java
[Docker] Connection with MySQL
Build Java development environment with WSL2 Docker VS Code
About Java 10 Docker support
Php settings with Docker
Let's scrape with Java! !!
Getting Started with Docker
Build Java with Wercker
Disposable PHP with Docker
Install Composer with Docker
Endian conversion with JAVA
Java: Start WAS with Docker and deploy your own application
[Java] Sample project for developing web applications with Spring Boot
Easy BDD with (Java) Spectrum?
Pytorch execution environment with Docker
Use Lambda Layers with Java
Java multi-project creation with Gradle
Getting Started with Java Collection
Use GDAL with Python with Docker
Java Config with Spring MVC
Basic Authentication with Java 11 HttpClient
Let's experiment with Java inlining
Run batch with docker-compose with Java batch
[Template] MySQL connection with Java
Rewrite Java try-catch with Optional
Install Java 7 with Homebrew (cask)
[Java] JSON communication with jackson
Deploy with EC2 / Docker / Laravel
Run TAO Core with Docker
Java to play with Function