Help us understand the problem. What is going on with this article?

sbt-native-packagerを使ってScalaPlayFrameworkのDockerImageを作る

More than 1 year has passed since last update.

前提

勉強会でsbt-native-packagerの存在を知った. PlayFrameworkのデプロイはdistコマンドでまとめた対象ファイルを反映していたけれど, もう少しシンプルにしたい. コンテナ単位で環境を丸ごとデプロイできると嬉しい. k8sでコンテナ単位でデプロイできたらなんかカッコイー!(やり方は知らない) デプロイ用DockerImageの作り方を検証する.

環境

  • OS: macOS Mojave Version 10.14
  • JDK: 1.8.0_172
  • Scala: 2.12.6
  • sbt: 1.2.1
  • PlayFramework: 2.6.18
  • Docker: 18.06.1-ce, build e68fc7a
  • DockerBaseImage: java:openjdk-8-jdk (Debian)

検証

PlayFrameworkの雛形プロジェクトを作る. playframework/play-scala-seed.g8参照.

sbt new playframework/play-scala-seed.g8

project/plugins.sbtにsbt-native-packagerを追加する. sbt/sbt-native-packager参照.

project/plugins.sbt
// https://github.com/sbt/sbt-native-packager
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.12")

build.sbtにDockerImage項目を設定する. Docker Plugin参照. Alpine系BaseImageだと起動できなかったのでDebian系BaseImageを採用した. 重たいけど.,, "is_cygwin not found" when using AshScriptPlugin & DockerPlugin from sbt-native-packager #8282参照. CI/CDツールでソースリポジトリのTag/CommitHashをversionに設定してDockerImageを作ると良さそう.

build.sbt
lazy val root = (project in file("."))
  .enablePlugins(PlayScala, JavaAppPackaging, DockerPlugin)
  .settings(
    organization := "com.example",
    name := "play2docker",
    version := "Tag",
    scalaVersion := "2.12.6",
  ).settings(
    dockerRepository := Some("usabarashi"),
    dockerBaseImage := "java:openjdk-8-jdk",
    dockerEntrypoint := Seq("/opt/docker/bin/play2docker"),
    dockerExposedPorts := Seq(9000),
  )

libraryDependencies += guice
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "3.1.2" % Test

conf/application.confにapplication secretを設定する.

$ sbt playUpdateSecret

PlayFrameworkのDockerImageを作成する. 'docker:publish'で直接pushしても良さそう.

$ sbt 'docker:publishLocal'
[info] Loading settings for project global-plugins from idea.sbt ...

〜(中略)〜

[success] Total time: 28 s, completed Oct 29, 2018 1:13:22 AM
[INFO] [10/29/2018 01:13:22.362] [Thread-3] [CoordinatedShutdown(akka://sbt-web)] Starting coordinated shutdown from JVM shutdown hook

$ docker images
REPOSITORY               TAG                  IMAGE ID            CREATED             SIZE
usabarashi/play2docker   Tag                  46477d63658d        5 minutes ago       681MB
java                     openjdk-8-jdk        d23bdf5b1b1b        21 months ago       643MB

$ 

PlayFrameworkのDockerImageからContainerを立ち上げる.

$ docker run -d -p 9000:9000 --name play2docker usabarashi/play2docker:Tag
7164854dab2030b5559494cdb59404f6ea20766e89c176ca52d879ee8dd2d7b3

$ docker ps
CONTAINER ID        IMAGE                               COMMAND                  CREATED             STATUS              PORTS                    NAMES
7164854dab20        usabarashi/play2docker:Tag          "/opt/docker/bin/pla…"   11 seconds ago      Up 10 seconds       0.0.0.0:9000->9000/tcp   play2docker

$ curl http://localhost:9000
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Welcome to Play</title>
        <link rel="stylesheet" media="screen" href="/assets/stylesheets/main.css">
        <link rel="shortcut icon" type="image/png" href="/assets/images/favicon.png">
    </head>
    <body>
  <h1>Welcome to Play!</h1>
      <script src="/assets/javascripts/main.js" type="text/javascript"></script>
    </body>
</html>

$

成功!

所感

これを書くために情報を整理していたら, PlayFrameworkのドキュメントにsbt-native-packgerの説明を見つけた.,, Dockerの説明がなかったから琴線に触れなかったのかな.,, 後は, インハウスのGitLab Container RegistryにDockerImageをpushして, Rancher? k8s? でなんか良い感じにデプロイできたら楽そう. 全然検証してないので全部妄想.

参考文献

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away