‹‹ All posts

Scala application dockerised

27 of April, 2017

There are tens of articles about dockerising Scala apps. Here is my take (or more of a note to future self).

In SBT - you can use sbt-native-packager to package it into docker image.

Just add plugin to project/plugins.sbt:

addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.1.5")

and enable it in build.sbt like this:

enablePlugins(AshScriptPlugin, DockerPlugin)

DockerPlugin enables building and publishing docker images. AshScriptPlugin allows to build using image, which do not have bash included (you probably can skip this one if not going to build on top of minimal alpine linux).

You don’t have to care about creating Dockerfile - plugin will do this work for you (yay!).

At the minimum you only need to specify which port to expose. There are more options, of course, see plugin documentation for others. Below is an example of such configuration in build.sbt (exposes, port, uses minimal image, sets version, repository and package names):

dockerBaseImage := "openjdk:8-jre-alpine"
dockerExposedPorts := Seq(8080)
dockerRepository in Docker := Some("asarturas")
packageName in Docker := "somapp"
version in Docker := "latest"

Once this is done:

  1. sbt docker:publishLocal will build and publish image into local environment. You can then use the package with a command like docker run --rm -p"8080:8080" asarturas/someapp
  2. sbt docker:publish is essentially the same as the above, with addition of publishing to the docker repository. By default it will attempt to publish to Docker Hub, so you will have to be logged into your account with docker login.

Pairing buddy is a sample app, built using this approach.

comments powered by Disqus