Creating Scala docker images with Google Jib and Maven
tl;dr:
Project template to dockerize a Scala application using Jib.
What’s Jib
Jib is a Google project to easily create optimized Docker images.
Main project goals:
Fast - Deploy your changes fast. Jib separates your application into multiple layers, splitting dependencies from classes. Now you don’t have to wait for Docker to rebuild your entire Java application - just deploy the layers that changed.
Reproducible - Rebuilding your container image with the same contents always generates the same image. Never trigger an unnecessary update again.
Daemonless - Reduce your CLI dependencies. Build your Docker image from within Maven or Gradle and push to any registry of your choice. No more writing Dockerfiles and calling docker build/push.
Jib consists of a Maven and Gradle but unfortunatly no SBT version.
Making it work with Scala
As SBT is not an option right now the integration has to happen either through Maven or Gradle. In this example we are using Maven.
If you want to continue using SBT you could build your artifact using SBT and then just have it as a dependency in a Maven/Docken project to build the final image.
This example will not be using SBT but Maven to build the Scala project and docker image creation.
Enabling the Jib plugin is straight forward. Simply add the jib plugin artifact in the build:plugin
section of the pom.xml
. To succesfully build an image the plugin needs some help with finding the main class of the Scala project. This can simply be fixed by adding mainClass
entry in the configuration
section of the plugin.
Final pom.xml
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>0.9.0</version>
<configuration>
<mainClass>com.example.QuickstartServer</mainClass>
</configuration>
</plugin>
Building a local docker image
mvn clean compile jib:dockerBuild
Running Application in Docker
docker run -p 8080:8080 scala-maven:1.0.0-SNAPSHOT
Output:
Server online at http://0.0.0.0:8080/
Further reading
Check out the Jib Maven plugin page to see all configuration parameters.