SlideShare a Scribd company logo
Ship your Scala code often
and easy with Docker
Marcus Lönnberg
marcus@lonnberg.nu
github.com/marcuslonnberg
@lonnbergm
e-bokföring
Ship your Scala code often and easy with Docker
Agenda
• Intro to Docker
• Building Docker images with sbt
• Manage Docker containers with Scala
• Docker.at(SpeedLedger)
Ship your Scala code often and easy with Docker
Platform
Open source
Client - server
REST API
Immutable images
Container virtualization
Traditional VMs Docker
Docker image
Dockerfile
Container
Docker image
Dockerfile
Container
> docker build --tag=myimage
> docker run myimage
Live demo
Building and shipping Docker images
Machine 1
Dockerfile + files
Machine x
> docker pull myimage
> docker run myimage
Registry
> docker build --tag=myimage
> docker push myimage
Machine xMachine x
Building Docker images from sbt
Manually building a Scala application
as a Docker image
1. Produce a fat JAR
2. Define a Dockerfile
3. Build a Docker image
0. Current sbt build
name := "fancy-service"

organization := "demo"



libraryDependencies ++= Seq(

"com.typesafe.akka" %% "akka-actor" % "2.3.9",

"io.spray" %% "spray-routing" % “1.3.3”,
...

)
build.sbt
1. sbt-assembly gives us a fat JAR
project/plugins.sbt
> sbt assembly
…
[info] Packaging out/fancy-service.jar ...
…
[success] Total time: 6 s
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.13.0")
2. Define a Dockerfile
FROM ubuntu
RUN apt-get update
RUN apt-get install -y openjdk-8-jre-headless
COPY fancy-service.jar /app/fancy-service.jar
WORKDIR /app
ENTRYPOINT java -jar fancy-service.jar
EXPOSE 8080
Dockerfile
2. Define a Dockerfile
FROM ubuntu
RUN apt-get update
RUN apt-get install -y openjdk-8
COPY fancy-service.jar
WORKDIR /app
ENTRYPOINT java -jar fancy-service.jar
EXPOSE 8080
Dockerfile
FROM java:8-jre
WORKDIR /app
EXPOSE 8080
ENTRYPOINT java -jar fancy-service.jar
COPY fancy-service.jar /app/fancy-service.jar
good
3. Building a Docker image
> docker build -t demo/fancy-service .
Sending build context to Docker daemon
Step 0 : FROM java:8-jre
---> 028f36974b77
Step 1 : WORKDIR /app
---> fb8108515091
Step 2 : EXPOSE 8080
---> 9512be4df795
Step 3 : ENTRYPOINT java -jar fancy-service.jar
---> 839dd75f5a96
Step 4 : COPY fancy-service.jar /app/fancy-service.jar
---> 948eb875c3b1
Removing intermediate container f9eeb9406fc7
Successfully built 948eb875c3b1
sbt-docker
sbt-docker
addSbtPlugin("se.marcuslonnberg" % "sbt-docker" % "1.1.0")
project/plugins.sbt
enablePlugins(DockerPlugin)
build.sbt
Dockerfile DSL
def jarFile: File
def jarName: String



new Dockerfile {

from("java:8-jre")

workDir("/app")

expose(8080)

entryPointRaw(s"java -jar $jarName")

copy(jarFile, s"/app/$jarName")

}
Putting it all together
docker <<= docker.dependsOn(assembly)



dockerfile in docker := {

val jarFile = (assemblyOutputPath in assembly).value

val jarName = name.value + ".jar"


new Dockerfile {

from("java:8-jre")

workDir("/app")

expose(8080)

entryPointRaw(s"java -jar $jarName")

copy(jarFile, s"/app/$jarName")

}

}
build.sbt
Let’s build an image
> sbt docker
…
[info] Packaging out/fancy-service.jar ...
…
[info] Step 0 : FROM java:8-jre
[info] ---> 028f36974b77
…
[info] Successfully built 30c376822ced
[info] Tagging image 30c376822ced with name: demo/fancy-service:0.1-SNAPSHOT
[info] Tagging image 30c376822ced with name: demo/fancy-service:latest
[success] Total time: 20 s
Image names
imageNames in docker := Seq(

ImageName("demo/fancy-service:" + version.value),

ImageName("demo/fancy-service:latest")

)
build.sbt
Immutable Dockerfile
def jarFile: File
def jarName: String



Dockerfile.empty

.from("java:8-jre")

.workDir("/app")

.expose(8080)

.entryPointRaw(s"java -jar $jarName")

.copy(jarFile, s"/app/$jarName")
Build options
buildOptions in docker := BuildOptions(

cache = true,

removeIntermediateContainers = BuildOptions.Remove.OnSuccess,

pullBaseImage = BuildOptions.Pull.Always

)
build.sbt
sbt-docker
• Define Dockerfiles with a DSL
• Dynamically name your images
• Push images to a registry
github.com/marcuslonnberg/sbt-docker
Manage Docker containers
from Scala
REST API
REST API
scala-docker
Live coding
Docker.at(SpeedLedger)
Build pipeline
Dev machine
Build server
Test
Production
Registry
git repo
> git push
> sbt dockerBuildAndPush
> docker pull image
Dev machine
Tagging Docker images with git
imageNames in docker := Seq(

ImageName("demo/fancy-service:" + git.gitHeadCommit.value.get),

ImageName("demo/fancy-service:" + git.gitCurrentBranch.value)

)
Labels with build info
val labels = Map(

"build.commitId" -> git.gitHeadCommit.value.get,

"build.branch" -> git.gitCurrentBranch.value,

"build.appName" -> name.value,

"build.buildTime" -> Platform.currentTime.toString

)



new Dockerfile {

…

label(labels)

…

}
Docker >= 1.6
Internal sbt plugin
• Adds common libraries
• Generates build info
• Configures logging
• Adds monitoring
• Sets good runtime properties
• Defines a Dockerfile template
Ship your Scala code often and easy with Docker
What we have learned
• Order instructions in Dockerfiles for fast builds
• Docker is very easy to use except for networking and 

file persistence
• Define own base images but use official when ones exist
• Building tools around Docker is easy and fun
Links
• github.com/marcuslonnberg/sbt-docker
• github.com/marcuslonnberg/scala-docker
• github.com/sbt/sbt-assembly
• github.com/sbt/sbt-native-packager
Thank you!
marcus@lonnberg.nu
github.com/marcuslonnberg
@lonnbergm

More Related Content

PDF
Continous delivery with sbt
PDF
Custom deployments with sbt-native-packager
PPT
An introduction to maven gradle and sbt
PDF
Simple Build Tool
PDF
Docker at Djangocon 2013 | Talk by Ken Cochrane
PPTX
Django via Docker
PDF
Containerizing a Web Application with Vue.js and Java
PDF
[Image Results] Java Build Tools: Part 2 - A Decision Maker's Guide Compariso...
Continous delivery with sbt
Custom deployments with sbt-native-packager
An introduction to maven gradle and sbt
Simple Build Tool
Docker at Djangocon 2013 | Talk by Ken Cochrane
Django via Docker
Containerizing a Web Application with Vue.js and Java
[Image Results] Java Build Tools: Part 2 - A Decision Maker's Guide Compariso...

What's hot (20)

PDF
Using Kubernetes for Continuous Integration and Continuous Delivery
PDF
Running Django on Docker: a workflow and code
PPTX
Installaling Puppet Master and Agent
PDF
SBT Crash Course
PDF
Gradle build tool that rocks with DSL JavaOne India 4th May 2012
PDF
Docker by Example - Basics
PDF
Using Kubernetes for Continuous Integration and Continuous Delivery. Java2days
PDF
Cloning Running Servers with Docker and CRIU by Ross Boucher
PDF
Deploying Docker (Provisioning /w Docker + Chef/Puppet) - DevopsDaysPGH
PPTX
ABCs of docker
PDF
Docker by Example - Quiz
PDF
Docker by Example - Basics
PDF
Packaging et déploiement d'une application avec Docker et Ansible @DevoxxFR 2015
PPTX
Docker Starter Pack
PPTX
SBT Concepts, part 2
PDF
A Hands-on Introduction to Docker
PDF
DevOps(4) : Ansible(2) - (MOSG)
PDF
Dockerfile
PDF
Sbt Concepts - Tips, Tricks, Sandbox, ... 02/2013
PDF
Docker Started
Using Kubernetes for Continuous Integration and Continuous Delivery
Running Django on Docker: a workflow and code
Installaling Puppet Master and Agent
SBT Crash Course
Gradle build tool that rocks with DSL JavaOne India 4th May 2012
Docker by Example - Basics
Using Kubernetes for Continuous Integration and Continuous Delivery. Java2days
Cloning Running Servers with Docker and CRIU by Ross Boucher
Deploying Docker (Provisioning /w Docker + Chef/Puppet) - DevopsDaysPGH
ABCs of docker
Docker by Example - Quiz
Docker by Example - Basics
Packaging et déploiement d'une application avec Docker et Ansible @DevoxxFR 2015
Docker Starter Pack
SBT Concepts, part 2
A Hands-on Introduction to Docker
DevOps(4) : Ansible(2) - (MOSG)
Dockerfile
Sbt Concepts - Tips, Tricks, Sandbox, ... 02/2013
Docker Started
Ad

Viewers also liked (20)

PDF
Scala, docker and testing, oh my! mario camou
PDF
Unsucking Error Handling with Futures
PDF
Continous delivery - lad koden flyde 2014
PDF
Automated Performance Testing With J Meter And Maven
PDF
Akka Http , Routes, Streams with Scala
PPTX
Using Jenkins and Jmeter to build a scalable Load Testing solution
PPT
Performance Testing With Jmeter
PDF
Akka http 2
ODP
An Introduction to Akka http
PDF
Practical Akka HTTP - introduction
PDF
Securing Microservices using Play and Akka HTTP
PDF
Building scalable rest service using Akka HTTP
PDF
Lessons Learned From Running Spark On Docker
PPTX
Building a Reactive RESTful API with Akka Http & Slick
PDF
Spring Boot Microservices vs Akka Actor Cluster
PDF
Jmeter Performance Testing
PDF
What's new in Zimbra Collaboration 8.7.x
PDF
Functional programming in Scala
PPTX
Scala - the good, the bad and the very ugly
PDF
Node.js vs Play Framework
Scala, docker and testing, oh my! mario camou
Unsucking Error Handling with Futures
Continous delivery - lad koden flyde 2014
Automated Performance Testing With J Meter And Maven
Akka Http , Routes, Streams with Scala
Using Jenkins and Jmeter to build a scalable Load Testing solution
Performance Testing With Jmeter
Akka http 2
An Introduction to Akka http
Practical Akka HTTP - introduction
Securing Microservices using Play and Akka HTTP
Building scalable rest service using Akka HTTP
Lessons Learned From Running Spark On Docker
Building a Reactive RESTful API with Akka Http & Slick
Spring Boot Microservices vs Akka Actor Cluster
Jmeter Performance Testing
What's new in Zimbra Collaboration 8.7.x
Functional programming in Scala
Scala - the good, the bad and the very ugly
Node.js vs Play Framework
Ad

Similar to Ship your Scala code often and easy with Docker (20)

PPTX
Into to Docker (Central PA Java User Group - 8/14/2017)
PPTX
How to _docker
PPTX
Fabricio - Docker deploy automation
PPTX
Docker Introductory workshop
PDF
DevOps Workflow: A Tutorial on Linux Containers
PPTX
Java microservicesdockerdockerhubusecase2
PDF
Clouds and Tools: Cheat Sheets & Infographics
PDF
How to Improve Your Image Builds Using Advance Docker Build
PPTX
Dockercompose
PDF
Microservices DevOps on Google Cloud Platform
PPTX
Build, Publish, Deploy and Test Docker images and containers with Jenkins Wor...
PPTX
How to Dockerize your Sitecore module
PDF
Play on Docker
PPTX
Baking docker using chef
PPTX
Baking Docker Using Chef - ChefConf 2015
PPTX
Docker for developers z java
PPTX
Introduction To Docker
PPTX
[Codelab 2017] Docker 기초 및 활용 방안
PPTX
Develop with docker 2014 aug
PDF
Docker From Scratch
Into to Docker (Central PA Java User Group - 8/14/2017)
How to _docker
Fabricio - Docker deploy automation
Docker Introductory workshop
DevOps Workflow: A Tutorial on Linux Containers
Java microservicesdockerdockerhubusecase2
Clouds and Tools: Cheat Sheets & Infographics
How to Improve Your Image Builds Using Advance Docker Build
Dockercompose
Microservices DevOps on Google Cloud Platform
Build, Publish, Deploy and Test Docker images and containers with Jenkins Wor...
How to Dockerize your Sitecore module
Play on Docker
Baking docker using chef
Baking Docker Using Chef - ChefConf 2015
Docker for developers z java
Introduction To Docker
[Codelab 2017] Docker 기초 및 활용 방안
Develop with docker 2014 aug
Docker From Scratch

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Approach and Philosophy of On baking technology
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
cuic standard and advanced reporting.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Advanced methodologies resolving dimensionality complications for autism neur...
Approach and Philosophy of On baking technology
Per capita expenditure prediction using model stacking based on satellite ima...
The Rise and Fall of 3GPP – Time for a Sabbatical?
cuic standard and advanced reporting.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Modernizing your data center with Dell and AMD
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Network Security Unit 5.pdf for BCA BBA.
Digital-Transformation-Roadmap-for-Companies.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
NewMind AI Monthly Chronicles - July 2025
The AUB Centre for AI in Media Proposal.docx
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Dropbox Q2 2025 Financial Results & Investor Presentation
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Encapsulation_ Review paper, used for researhc scholars
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...

Ship your Scala code often and easy with Docker

  • 1. Ship your Scala code often and easy with Docker Marcus Lönnberg marcus@lonnberg.nu github.com/marcuslonnberg @lonnbergm
  • 4. Agenda • Intro to Docker • Building Docker images with sbt • Manage Docker containers with Scala • Docker.at(SpeedLedger)
  • 6. Platform Open source Client - server REST API Immutable images Container virtualization
  • 9. Docker image Dockerfile Container > docker build --tag=myimage > docker run myimage
  • 11. Building and shipping Docker images Machine 1 Dockerfile + files Machine x > docker pull myimage > docker run myimage Registry > docker build --tag=myimage > docker push myimage Machine xMachine x
  • 13. Manually building a Scala application as a Docker image 1. Produce a fat JAR 2. Define a Dockerfile 3. Build a Docker image
  • 14. 0. Current sbt build name := "fancy-service"
 organization := "demo"
 
 libraryDependencies ++= Seq(
 "com.typesafe.akka" %% "akka-actor" % "2.3.9",
 "io.spray" %% "spray-routing" % “1.3.3”, ...
 ) build.sbt
  • 15. 1. sbt-assembly gives us a fat JAR project/plugins.sbt > sbt assembly … [info] Packaging out/fancy-service.jar ... … [success] Total time: 6 s addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.13.0")
  • 16. 2. Define a Dockerfile FROM ubuntu RUN apt-get update RUN apt-get install -y openjdk-8-jre-headless COPY fancy-service.jar /app/fancy-service.jar WORKDIR /app ENTRYPOINT java -jar fancy-service.jar EXPOSE 8080 Dockerfile
  • 17. 2. Define a Dockerfile FROM ubuntu RUN apt-get update RUN apt-get install -y openjdk-8 COPY fancy-service.jar WORKDIR /app ENTRYPOINT java -jar fancy-service.jar EXPOSE 8080 Dockerfile FROM java:8-jre WORKDIR /app EXPOSE 8080 ENTRYPOINT java -jar fancy-service.jar COPY fancy-service.jar /app/fancy-service.jar good
  • 18. 3. Building a Docker image > docker build -t demo/fancy-service . Sending build context to Docker daemon Step 0 : FROM java:8-jre ---> 028f36974b77 Step 1 : WORKDIR /app ---> fb8108515091 Step 2 : EXPOSE 8080 ---> 9512be4df795 Step 3 : ENTRYPOINT java -jar fancy-service.jar ---> 839dd75f5a96 Step 4 : COPY fancy-service.jar /app/fancy-service.jar ---> 948eb875c3b1 Removing intermediate container f9eeb9406fc7 Successfully built 948eb875c3b1
  • 20. sbt-docker addSbtPlugin("se.marcuslonnberg" % "sbt-docker" % "1.1.0") project/plugins.sbt enablePlugins(DockerPlugin) build.sbt
  • 21. Dockerfile DSL def jarFile: File def jarName: String
 
 new Dockerfile {
 from("java:8-jre")
 workDir("/app")
 expose(8080)
 entryPointRaw(s"java -jar $jarName")
 copy(jarFile, s"/app/$jarName")
 }
  • 22. Putting it all together docker <<= docker.dependsOn(assembly)
 
 dockerfile in docker := {
 val jarFile = (assemblyOutputPath in assembly).value
 val jarName = name.value + ".jar" 
 new Dockerfile {
 from("java:8-jre")
 workDir("/app")
 expose(8080)
 entryPointRaw(s"java -jar $jarName")
 copy(jarFile, s"/app/$jarName")
 }
 } build.sbt
  • 23. Let’s build an image > sbt docker … [info] Packaging out/fancy-service.jar ... … [info] Step 0 : FROM java:8-jre [info] ---> 028f36974b77 … [info] Successfully built 30c376822ced [info] Tagging image 30c376822ced with name: demo/fancy-service:0.1-SNAPSHOT [info] Tagging image 30c376822ced with name: demo/fancy-service:latest [success] Total time: 20 s
  • 24. Image names imageNames in docker := Seq(
 ImageName("demo/fancy-service:" + version.value),
 ImageName("demo/fancy-service:latest")
 ) build.sbt
  • 25. Immutable Dockerfile def jarFile: File def jarName: String
 
 Dockerfile.empty
 .from("java:8-jre")
 .workDir("/app")
 .expose(8080)
 .entryPointRaw(s"java -jar $jarName")
 .copy(jarFile, s"/app/$jarName")
  • 26. Build options buildOptions in docker := BuildOptions(
 cache = true,
 removeIntermediateContainers = BuildOptions.Remove.OnSuccess,
 pullBaseImage = BuildOptions.Pull.Always
 ) build.sbt
  • 27. sbt-docker • Define Dockerfiles with a DSL • Dynamically name your images • Push images to a registry github.com/marcuslonnberg/sbt-docker
  • 34. Build pipeline Dev machine Build server Test Production Registry git repo > git push > sbt dockerBuildAndPush > docker pull image Dev machine
  • 35. Tagging Docker images with git imageNames in docker := Seq(
 ImageName("demo/fancy-service:" + git.gitHeadCommit.value.get),
 ImageName("demo/fancy-service:" + git.gitCurrentBranch.value)
 )
  • 36. Labels with build info val labels = Map(
 "build.commitId" -> git.gitHeadCommit.value.get,
 "build.branch" -> git.gitCurrentBranch.value,
 "build.appName" -> name.value,
 "build.buildTime" -> Platform.currentTime.toString
 )
 
 new Dockerfile {
 …
 label(labels)
 …
 } Docker >= 1.6
  • 37. Internal sbt plugin • Adds common libraries • Generates build info • Configures logging • Adds monitoring • Sets good runtime properties • Defines a Dockerfile template
  • 39. What we have learned • Order instructions in Dockerfiles for fast builds • Docker is very easy to use except for networking and 
 file persistence • Define own base images but use official when ones exist • Building tools around Docker is easy and fun
  • 40. Links • github.com/marcuslonnberg/sbt-docker • github.com/marcuslonnberg/scala-docker • github.com/sbt/sbt-assembly • github.com/sbt/sbt-native-packager