12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SpringBoot + Kotlin + Docker を使ってみる

Last updated at Posted at 2018-12-16

なにこれ

dockerとKotlinを使ってみたかった
https://training.play-with-docker.com/ でdockerを学び
https://spring.io/guides/tutorials/spring-boot-kotlin/ でspringとkotlin
https://spring.io/guides/gs/spring-boot-docker/ でspringとdockerの組み合わせ方を学んだ
それのまとめ。

開発環境

$ gradle -v
Gradle 4.4.1
$ java -version
java version "1.8.0_91"
$ docker -v
Docker version 18.09.0, build 4d60db4

雛形作り&起動確認

$ mkdir boot-sample-kotlin-with-docker && cd boot-sample-kotlin-with-docker
$ curl https://start.spring.io/starter.zip -d type=gradle-project -d language=kotlin -d style=web  -d packageName=sample -d name=sample -o boot-sample-kotlin-with-docker.zip
$ unzip boot-sample-kotlin-with-docker.zip && rm boot-sample-kotlin-with-docker.zip
$ ./gradlew build && java -jar build/libs/demo-0.0.1-SNAPSHOT.jar

Controllerの追加&動作確認

  • Controllerの追加
src/main/kotlin/sample/Controller.kt
package sample

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class Controller {

    @GetMapping(path = ["hello"])
    fun hello(): String {
        return "Hello Kotlin, Hello Docker."
    }
}
  • 動作確認
$ ./gradlew build && java -jar build/libs/demo-0.0.1-SNAPSHOT.jar
$ curl localhost:8080/hello
Hello Kotlin, Hello Docker.

Dockerfileの作成

https://spring.io/guides/gs/spring-boot-docker/ のDockerfileとbuild.gradleの設定だと下記エラーになる。Kotlinを使用しているためmain methodが見つからないっぽい。

Error: Main method not found in class sample.SampleApplication, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

対応方法がわからなかったので、こっち( https://cloud.google.com/community/tutorials/kotlin-springboot-container-engine ) で紹介されているやり方でやってみる。

FROM openjdk:8-jdk-alpine
VOLUME /tmp
RUN mkdir /work
COPY . /work
WORKDIR /work
RUN /work/gradlew build
RUN mv /work/build/libs/*.jar /work/app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/work/app.jar"]
$ docker build --no-cache -t demo .
$ docker image ls
REPOSITORY                       TAG                 IMAGE ID            CREATED              SIZE
demo                             latest              834f1d599273        About a minute ago   620MB

最後の動作確認


$ docker run -p 8081:8080 demo
$ curl localhost:8081/hello
Hello Kotlin, Hello Docker.

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?