Scala

Dockerize your Scala application

Photo of Emmanouil GkatziourasEmmanouil GkatziourasMay 15th, 2018Last Updated: May 14th, 2018
0 123 1 minute read

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.WarnaddSbtPlugin("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 the openjdk alpine as a base image.

FROM openjdk:8-jre-alpineADD target/scala-**/your-fat-jar app.jarENTRYPOINT ["java","-jar","/app.jar"]

The above approach works ok and gives the control needed to customize your build process. For a more bootstraping experience you can use thesbt native packager.

All you need to do is to add the plugin to project/plugins.sbt file.

logLevel := Level.WarnaddSbtPlugin("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.

Published on Java Code Geeks with permission by Emmanouil Gkatziouras, partner at ourJCG program. See the original article here:Dockerize your Scala application

Opinions expressed by Java Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Tags
Photo of Emmanouil GkatziourasEmmanouil GkatziourasMay 15th, 2018Last Updated: May 14th, 2018
0 123 1 minute read
Photo of Emmanouil Gkatziouras

Emmanouil Gkatziouras

He is a versatile software engineer with experience in a wide variety of applications/services.He is enthusiastic about new projects, embracing new technologies, and getting to know people in the field of software.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.