SlideShare a Scribd company logo
1 of 41
Download to read offline
Ship your Scala code often
and easy with Docker
Marcus Lönnberg
marcus@lonnberg.nu
github.com/marcuslonnberg
@lonnbergm
e-bokföring
Agenda
• Intro to Docker
• Building Docker images with sbt
• Manage Docker containers with Scala
• Docker.at(SpeedLedger)
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
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

What's hot

Installaling Puppet Master and Agent
Installaling Puppet Master and AgentInstallaling Puppet Master and Agent
Installaling Puppet Master and Agent
Ranjit Avasarala
 

What's hot (20)

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

Viewers also liked

Automated Performance Testing With J Meter And Maven
Automated  Performance  Testing With  J Meter And  MavenAutomated  Performance  Testing With  J Meter And  Maven
Automated Performance Testing With J Meter And Maven
PerconaPerformance
 
Akka Http , Routes, Streams with Scala
Akka Http , Routes, Streams with ScalaAkka Http , Routes, Streams with Scala
Akka Http , Routes, Streams with Scala
Jerry Kuru
 
Jmeter Performance Testing
Jmeter Performance TestingJmeter Performance Testing
Jmeter Performance Testing
Atul Pant
 

Viewers also liked (20)

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

Similar to Ship your Scala code often and easy with Docker

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

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

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 

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
  • 3.
  • 4. Agenda • Intro to Docker • Building Docker images with sbt • Manage Docker containers with Scala • Docker.at(SpeedLedger)
  • 5.
  • 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
  • 38.
  • 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