Home Education Docker Tutorial: Dockerizing Scala

Docker Tutorial: Dockerizing Scala

Author

Date

Category

Prerequisites

A working docker installation (boot2docker is fine)

Typesafeโ€™s activator (or use ./activator found at the root of the git-repoโ€Šโ€”โ€Šit will download the dependencies)

You can find/fork the sourcecode for all of the following on Github

Intro

The sbt plugin sbt-native-packager provides a really easy way to deploy your sbt project as a docker container.

In the following we will create a docker running a tiny scala helloWorld.

To use the native-packager we just add this line to your project/plugins.sbt

addSbtPlugin(โ€œcom.typesafe.sbtโ€ % โ€œsbt-native-packagerโ€ % โ€œ1.0.3โ€)

and in the build.sbt put:

enablePlugins(JavaAppPackaging)

enablePlugins(DockerPlugin)

Publishing

At this point our application is ready to run and you can deploy your docker by running

sbt docker:publishLocal

in your terminal. (This might take a while depending on how many layers you have to download)

Letโ€™s verify the process by executing:

docker images

and we should see your newly created docker along with the base image (โ€œjava/latestโ€)

We can run it by executing:

docker run -it sbt-docker-example:1.0

and โ€œHello, world!โ€ should be printed.

Optimizing/Customizing

There are a few settings the plugins exposes and we can override. You can find a list here.

Our current container occupies quite a lot of space ( > 800 MB) and we will try (and succeed ๐Ÿ˜› ) to get it smaller. The current size is mostly because of the base imageโ€Šโ€”โ€Šโ€œjava:latestโ€โ€Šโ€”โ€Šwhich is the packagerโ€™s default (it uses OpenJDK). We can override with any other image that provides a java installation.

For our example we will use frolvladโ€™s image which is significantly smaller and uses oracle-java. We add the following line to our build.sbt:

dockerBaseImage := โ€œfrolvlad/alpine-oraclejdk8โ€

Publishing the container now results in a much smaller image. However itโ€™s brokenโ€ฆ

sbt docker:publishLocal && docker run -it sbt-docker-example:1.0

results in:

env: canโ€™t execute โ€˜bashโ€™: No such file or directory

The native-packager depends on a working bash installation in the container. So letโ€™s install it.

But first letโ€™s take a look at whatโ€™s happening under the hood. We would like to know how the image is being built. Letโ€™s take a look at the docker comands by running

sbt โ€œshow dockerCommandsโ€

gives us:

List(Cmd(FROM,frolvlad/alpine-oraclejdk8), Cmd(WORKDIR,/opt/docker), Cmd(ADD,opt /opt), ExecCmd(RUN,List(chown, -R, daemon:daemon, .)), Cmd(USER,daemon), ExecCmd(ENTRYPOINT,List(bin/sbt-docker-example)), ExecCmd(CMD,List()))

If we blank out the scala parts (Cmd,List and ExecCmd) this looks very similar to comands we find in a Dockerfile.

We decide that the right place to install bash is right after the FROM command. Letโ€™s override the dockerComands in our build.sbt by adding:

dockerCommands := dockerCommands.value.flatMap{
case cmd@Cmd(โ€œFROMโ€,_) => List(cmd, Cmd(โ€œRUNโ€, โ€œapk update && apk add bashโ€))
case other => List(other)
}

We could look at the command sequence again but letโ€™s just be bold an run the docker:

sbt docker:publishLocal && docker run -it sbt-docker-example:1.0

Et voilร , โ€œHello, world!โ€ is back. And the new image size is now < 200 MB


Edit: We just published an ebook: โ€œThe Ultimate Guide to Code Reviewโ€ based on a survey of 680+ developers. Enjoy!

About Codacy

Codacy is used by thousands of developers to analyze billions of lines of code every day!

Getting started is easy โ€“ and free! Just use your  GitHub, Bitbucket or Google account to sign up.

GET STARTED

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

Recent posts

How to implement coding standards in your organization

Coding standards are a set of guidelines and best practices designed to ensure that your code is consistent and readable, making...

Leading Your Team to Engineering Excellence

On March 7th, we did a webinar called Leading Your Team to Engineering Excellence. Guest speaker Steve Berczuk, Lead Software Engineer...

3 popular C# style guides that will help your team write better code

C# is a popular programming language developed by Microsoft, and you can use it for developing web applications, games, and more....

February Product Update ๐Ÿš€

Hi there ๐Ÿ‘‹, Here are a few updates from February with very exciting news, so keep on...

Velocity vs. Cycle Time: understanding the key differences

Velocity and Cycle time are two standard metrics to measure the efficiency and effectiveness of software development teams. They help you...