DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • 5 Simple Tips to Keep Dockerized Apps Secure
  • A Beginner's Guide to Docker Compose
  • Expert Techniques to Trim Your Docker Images and Speed Up Build Times
  • How To Use the Node Docker Official Image

Trending

  • Observability in Spring Boot 4
  • How AI Is Rewriting Full-Stack Java Systems: Practical Patterns with Spring Boot, Kafka and WebSockets
  • Product-Led Software Delivery: Intelligent Platforms for DevOps at Scale
  • Genkit Middleware: Intercept, Extend, and Harden your Gen AI Pipelines
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Dockerizing Your Scala App

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.

By 
Emmanouil Gkatziouras user avatar
Emmanouil Gkatziouras
DZone Core CORE ·
May. 15, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
19.0K Views

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.

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.

Scala (programming language) app application Docker (software) JAR (file format) Command (computing) Build (game engine) OpenJDK

Published at DZone with permission of Emmanouil Gkatziouras. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • 5 Simple Tips to Keep Dockerized Apps Secure
  • A Beginner's Guide to Docker Compose
  • Expert Techniques to Trim Your Docker Images and Speed Up Build Times
  • How To Use the Node Docker Official Image

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook