marcuslonnberg / sbt-docker

Create Docker images directly from sbt

Version Matrix


sbt-docker is an sbt plugin that builds and pushes Docker images for your project.

Build Status Maven Central


  • sbt
  • Docker


Add sbt-docker as a dependency in project/plugins.sbt:

addSbtPlugin("se.marcuslonnberg" % "sbt-docker" % "1.8.2")

Getting started

Below are some documentation on the sbt tasks and settings in the plugin.

This blog post gives a good introduction to the basics of sbt-docker: Dockerizing your Scala apps with sbt-docker

Also, take a look at the example projects.


Start by enabling the plugin in your build.sbt file:


This sets up some settings with default values and adds tasks such as docker which builds a Docker image. The only required setting that is left to define is dockerfile in docker.


If you want your Dockerfile to contain one or several artifacts (such as JAR files) that your project generates, then you must make the docker task depend on the tasks that generate them. It could for example be with the package task or with tasks from plugins such as sbt-assembly.

Defining a Dockerfile

In order to produce a Docker image a Dockerfile must be defined. It should be defined at the dockerfile in docker key. There is a mutable and an immutable Dockerfile class available, both provides a DSL which resembles the plain text Dockerfile format. The mutable class is default and is used in the following examples.

Example with the sbt-assembly plugin:

dockerfile in docker := {
  // The assembly task generates a fat JAR file
  val artifact: File = assembly.value
  val artifactTargetPath = s"/app/${}"

  new Dockerfile {
    add(artifact, artifactTargetPath)
    entryPoint("java", "-jar", artifactTargetPath)

Example with sbt-native-packager:

enablePlugins(sbtdocker.DockerPlugin, JavaAppPackaging)

dockerfile in docker := {
  val appDir: File = stage.value
  val targetDir = "/app"

  new Dockerfile {
    copy(appDir, targetDir, chown = "daemon:daemon")

Example with the sbt package task.

dockerfile in docker := {
  val jarFile: File = sbt.Keys.`package`.in(Compile, packageBin).value
  val classpath = (managedClasspath in Compile).value
  val mainclass =, packageBin).value.getOrElse(sys.error("Expected exactly one main class"))
  val jarTarget = s"/app/${jarFile.getName}"
  // Make a colon separated classpath with the JAR file
  val classpathString ="/app/" + _.getName)
    .mkString(":") + ":" + jarTarget
  new Dockerfile {
    // Base image
    // Add all files on the classpath
    add(classpath.files, "/app/")
    // Add the JAR file
    add(jarFile, jarTarget)
    // On launch run Java with the classpath and the main class
    entryPoint("java", "-cp", classpathString, mainclass)

Example with a Dockerfile in the filesystem.

dockerfile in docker := NativeDockerfile(file("subdirectory") / "Dockerfile")

Have a look at DockerfileExamples for different ways of defining a Dockerfile.

Building an image

To build an image use the docker task. Simply run sbt docker from your prompt or docker in the sbt console.

Pushing an image

An image that have already been built can be pushed with the dockerPush task. To both build and push an image use the dockerBuildAndPush task.

The imageNames in docker key is used to determine which image names to push.

Custom image names

You can specify the names / tags you want your image to get after a successful build with the imageNames in docker key of type Seq[sbtdocker.ImageName].


imageNames in docker := Seq(
  // Sets the latest tag

  // Sets a name with a tag that contains the project version
    namespace = Some(organization.value),
    repository = name.value,
    tag = Some("v" + version.value)

Build options

Use the key buildOptions in docker to set build options.


buildOptions in docker := BuildOptions(
  cache = false,
  removeIntermediateContainers = BuildOptions.Remove.Always,
  pullBaseImage = BuildOptions.Pull.Always,
  additionalArguments = Seq("--add-host", "", "--compress")

Build arguments

Use the key dockerBuildArguments in docker to set build arguments.


dockerBuildArguments in docker := Map(
  "KEY" -> "value",

dockerfile in docker := {
  new Dockerfile {
    // ...
    env("KEY" -> "$KEY", "CREDENTIALS" -> "$CREDENTIALS")
    // ...

BuildKit support

Images can be built with BuildKit by enabling it in the daemon configuration or by passing the environment variable DOCKER_BUILDKIT=1 to sbt.

Auto packaging JVM applications

If you have a standalone JVM application that you want a simple Docker image for. Then you can use dockerAutoPackageJavaApplication(fromImage, exposedPorts, exposedVolumes, username) which will setup some settings for you, including a Dockerfile. Its very basic, so if you have more advanced needs then define your own Dockerfile.