Dockerize Play Framefork Application and Deploy to GitLab Registry

為了將 Play Framefork 打包成 Docker image,這邊會使用到 sbt-native-packager,以下附上懶人包。

Code

build.sbt

maintainer in Docker := "Rammus Xu"
version in Docker := "latest"

dockerExposedPorts := Seq(9000, 9443)
dockerRepository := Some("registry.gitlab.com/my-gitlab-project")

// To resolve docker:stage not build swagger asset
(stage in Docker) <<= (stage in Docker).dependsOn(swagger)

Build and Deploy

Build docker image and test in local.

sbt docker:publishLocal
docker run -p 9002:9000 \
--name my-app \
-e DB_URL="jdbc:mysql://mysql/my_database?characterEncoding=UTF-8" \
-e MEMCACHED_HOST="memcached:11211" \
--link mysql:mysql --link memcached:memcached \
--rm \
registry.gitlab.com/my-gitlab-project:latest -Dconfig.resource=prod.conf

Push to GitLab registry.

docker login registry.gitlab.com
docker push registry.gitlab.com/my-gitlab-project:latest

Run Freemium, Download from gitlab.com

docker login registry.gitlab.com
docker run -p 9002:9000 \
--name free \
-e FREEMIUM_DB_URL="jdbc:mysql://mysql/freemium_fly?characterEncoding=UTF-8" \
-e FREEMIUM_MEMCACHED_HOST="memcached:11211" \
--link mysql:mysql --link memcached:memcached \
--rm \
registry.gitlab.com/repaas/backend/freemium:latest -Dconfig.resource=prod.conf

因為我們有使用 Swagger plugin,所以可以直接使用瀏覽器打開 http://localhost:9002/docs/swagger-ui/index.html?url=/assets/swagger.json

Reference

Troubleshooting

sbt docker:stage 不會更新 swagger

https://github.com/iheartradio/play-swagger/issues/114

需要在 docker:stage append swagger

(stage in Docker) <<= (stage in Docker).dependsOn(swagger)