Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Dockerizing Your Scala App

DZone's Guide to

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.

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

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.

sbt docker:publishLocal


This command will build your application, include the binaries needed to the JAR, containerize your application, and publish it to your local Maven repo.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
java ,dockerizing ,scala ,dockerfile ,build tool ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}