LoginSignup
12
12

More than 5 years have passed since last update.

JibでJavaアプリケーションを簡単にDocker化

Posted at

2018年7月11日にGoogleからJibが公開されました。JibはJavaアプリケーションを自動的にDockerコンテナ化してくれるOSSです。MavenとGradleのプラグインとして公開されており、プラグインとしてJibを登録するだけで簡単に利用できます。また、自分でDockerfileなどを書く必要がまったくなく、Jibが自動的にイメージを作ってくれます。今まではDockerfileやdocker-compose.ymlを書いてイメージをビルドしてましたが、mvnかgradleでビルドするだけで自動でイメージをビルドしれくれるのでとても楽ですね。この記事では、Springのアプリケーション(Hello world)をMavenプロジェクトで作り、dockerからアプリケーションを見るまで紹介します。

Mavenプロジェクト作成

SPRING INITIALIZRでまずはSpringの雛形を作ります。

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>

次にJibの設定を追加します。

<!-- 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>

build部分にJibのプラグインを追加します。

あとは、eclipseやIntellijで取り込むだけです。

Javaアプリケーション

アプリケーション側は今回は、Hello Worldのみ

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

JibでDockerイメージを作成します

$ mvn compile jib:build

成功するとDockerイメージが生成されています。


$ docker images
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
hellojib                                        latest              16ab4943a4d8        48 years ago        138MB

Dockerイメージ名は、pom.xml内のconfigurationで設定できます。今回はimageにhellojibを設定してます。

<configuration>
    <to>
        <image>hellojib</image>
    </to>
</configuration>

あとは、docker run --name hellojib -d -p 8080:8080 hellojibDといった感じDockerでJavaアプリケーションが起動できます。

12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12