Dockerizing Your Scala App
Here's a step-by-step guide that details how to containerize your Scala apps with Docker, including how to set up your Dockerfile.
Join the DZone community and get the full member experience.Join For Free
Dockerizing a Scala application is pretty easy.
The first concern is creating a fat JAR. Now, we all come from different backgrounds, including Maven/Gradle and different plugins that handle this issue. If you use sbt, the way to go is to use the sbt-assembly plugin.
To use it, we should add it to our project/plugins.sbt file. If the file does not exist, create it.
logLevel := Level.Warn addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.6")
So by executing...
sbt clean assembly
...we will end up with a fat JAR located at the target/scala-**/**.jar path.
Now the easy part is putting our application inside Docker. Thus, a Dockerfile is needed.
We will use OpenJDK Alpine as a base image.
FROM openjdk:8-jre-alpine ADD target/scala-**/your-fat-jar app.jar ENTRYPOINT ["java","-jar","/app.jar"]
The above approach works all right and gives you the control needed to customize your build process. For a more bootstrapped experience, you can use the sbt native packager.
All you need to do is to add the plugin to the project/plugins.sbt file.
logLevel := Level.Warn addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.4")
Then we specify the main class of our application and enable the Java and Docker plugins from the native packager at the build.sbt file.
mainClass in Compile := Some("your.package.MainClass") enablePlugins(JavaAppPackaging) enablePlugins(DockerPlugin)
The next step is to issue the sbt command.
This command will build your application, include the binaries needed to the JAR, containerize your application, and publish it to your local Maven repo.
Published at DZone with permission of Emmanouil Gkatziouras, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.