试着使用SpringBoot + Kotlin + Docker
这是什么
我想尝试使用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の追加
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
使用Kotlin编写的Dockerfile和build.gradle的设置会导致以下错误,提示找不到主方法。
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.
请谅解我只能提供一种选项:
– 参考一下
-
- docker
https://training.play-with-docker.com/
https://cloud.google.com/community/tutorials/kotlin-springboot-container-engine
spring
https://spring.io/guides/tutorials/spring-boot-kotlin/
https://spring.io/guides/gs/spring-boot-docker/
同じようなことをやってる人がいた
https://qiita.com/yumatsud/items/330fc8a8fd3ddb88b830