LoginSignup
6
4

More than 3 years have passed since last update.

jib-maven-pluginを使ってSpringBootアプリをイメージ化してDockerで起動

Last updated at Posted at 2019-12-22

はじめに

この記事は、Java Advent Calendar 2019 - Qiita 23日目の記事です。

皆さん、コンテナ使ってますか??
Googleが公開しているjib-maven-pluginを使用すれば、Javaで作成されたアプリを簡単にDockerイメージにすることができます。
今回はSpringBoot・Mavenを使用したSpringBootアプリケーションのイメージビルド及びコンテナ立ち上げまでやってみます(本当はAWSデプロイまでやりたかったけど時間がないので断念)

環境

  • Java8
  • SpringBoot2.1.9.RELEASE
  • Maven3.6.3
  • jib-maven-plugin1.8.0
  • Docker19.03.5

SpringBootアプリケーションを構築する

SpringBoot・Mavenで構築されたプロジェクトを作成します。
Springから提供されているSpring Initializrを使用すれば簡単にアプリケーションを構築することができます。

使用方法はこちら(後日使用方法をアップします)

サンプルControllerの追加

Webアプリケーションを今回は作成するので、サンプルのController及びhtmlを追加します。

IndexController

@Controller
public class IndexController {

    @GetMapping("/")
    public ModelAndView get(ModelAndView mav) {
        mav.setViewName("index");
        return mav;
    }
}

index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>JibSampleApp</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
    integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
    crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
    integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
    crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script
    src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
    integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
    crossorigin="anonymous"></script>

</head>
<body>
    <div class="container">
        <div class="row">
            <div class="jumbotron">
                <h1>JibSampleApp</h1>
                <p>this app is JibSampleApp</p>
            </div>
            <div class="panel panel-success">
                <div class="panel-heading">blank</div>
                <div class="panel-body"></div>
            </div>
        </div>
    </div>
</body>
</html>

プラグインを設定

上記で作成したSpringBootアプリケーションのpomに以下のプラグインを追加します。

pom.xml

<plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>jib-maven-plugin</artifactId>
    <version>1.8.0</version>
    <configuration>
        <to>
            <image>jibsampleimage</image>
        </to>
    </configuration>
</plugin>

build実行

Dockerが起動している状態で、
mvn compile jib:dockerBuild
を実行することで、SpringBootアプリケーションのイメージbuildが実行されます。
Windowsなどで管理者権限を剥奪されている場合、うまく動作しないことがあるので、その場合は一時的に管理者権限を付与する必要があります。

buildがうまくいくと、Dockerのイメージ一覧の中に作成したイメージがあることが確認できます。
docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
jibsampleimage      latest              bcb3c6749044        49 years ago        161MB

Dockerコンテナ起動

上記でbuildしたイメージをもとにDockerコンテナを作成し、実行します。
以下のコマンドを入力することで、ローカルの8080ポートとDockerの8080をマッピングし、ブラウザからアクセスし確認することができます。
docker run -p 8080:8080 -it jibsampleimage

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.9.RELEASE)

2019/12/18 14:11:53.177 [main] INFO   c.a.j.s.JibSampleApp Starting JibSampleApp on be8d974734e4 with PID 1 (/app/classes started by root in /)
2019/12/18 14:11:53.189 [main] INFO   c.a.j.s.JibSampleApp No active profile set, falling back to default profiles: default
2019/12/18 14:11:56.123 [main] WARN   o.m.s.m.ClassPathMapperScanner No MyBatis mapper was found in '[com.atu496.jib.sample]' package. Please check your configuration.
2019/12/18 14:11:58.065 [main] INFO   o.s.b.w.e.t.TomcatWebServer Tomcat initialized with port(s): 8080 (http)
2019/12/18 14:11:58.166 [main] INFO   o.a.c.h.Http11NioProtocol Initializing ProtocolHandler ["http-nio-8080"]
2019/12/18 14:11:58.213 [main] INFO   o.a.c.c.StandardService Starting service [Tomcat]
2019/12/18 14:11:58.217 [main] INFO   o.a.c.c.StandardEngine Starting Servlet engine: [Apache Tomcat/9.0.26]
2019/12/18 14:11:58.535 [main] INFO   o.a.c.c.C.[.[.[/] Initializing Spring embedded WebApplicationContext
2019/12/18 14:11:58.537 [main] INFO   o.s.w.c.ContextLoader Root WebApplicationContext: initialization completed in 5194 ms
2019/12/18 14:11:59.277 [main] INFO   o.s.s.c.ThreadPoolTaskExecutor Initializing ExecutorService 'applicationTaskExecutor'
2019/12/18 14:11:59.532 [main] INFO   o.s.b.a.w.s.WelcomePageHandlerMapping Adding welcome page template: index
2019/12/18 14:12:00.865 [main] INFO   o.a.c.h.Http11NioProtocol Starting ProtocolHandler ["http-nio-8080"]
2019/12/18 14:12:01.033 [main] INFO   o.s.b.w.e.t.TomcatWebServer Tomcat started on port(s): 8080 (http) with context path ''
2019/12/18 14:12:01.050 [main] INFO   c.a.j.s.JibSampleApp Started JibSampleApp in 9.135 seconds (JVM running for 10.809)

実際にlocalhost:8080にアクセスすると。。。

スクリーンショット 2019-12-18 23.14.07.png

起動できていることが確認できると思います。

終わりに

SpringBootアプリケーションの簡単なイメージの作成及びコンテナ起動することができました。
ただ、これはまだローカルでの話なので、時間ができ次第AWS・GCP・Azureのコンテナ実行環境へのデプロイ方法をまとめる予定です(本来はそっちがメインだった。。。)
今回作成したソースコードはGithub上にアップしています。

6
4
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
6
4