项目作者: marcuslonnberg

项目描述 :
Create Docker images directly from sbt
高级语言: Scala
项目地址: git://github.com/marcuslonnberg/sbt-docker.git
创建时间: 2014-01-12T19:43:03Z
项目社区:https://github.com/marcuslonnberg/sbt-docker

开源协议:MIT License

下载


sbt-docker

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

Maven Central

Requirements

  • sbt
  • Docker

Setup

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

  1. addSbtPlugin("se.marcuslonnberg" % "sbt-docker" % "1.11.0")

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.

Usage

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

  1. enablePlugins(DockerPlugin)

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 docker / dockerfile.

Artifacts

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 docker / dockerfile 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:

  1. docker / dockerfile := {
  2. // The assembly task generates a fat JAR file
  3. val artifact: File = assembly.value
  4. val artifactTargetPath = s"/app/${artifact.name}"
  5. new Dockerfile {
  6. from("openjdk:8-jre")
  7. add(artifact, artifactTargetPath)
  8. entryPoint("java", "-jar", artifactTargetPath)
  9. }
  10. }

Example with sbt-native-packager:

  1. enablePlugins(sbtdocker.DockerPlugin, JavaAppPackaging)
  2. docker / dockerfile := {
  3. val appDir: File = stage.value
  4. val targetDir = "/app"
  5. new Dockerfile {
  6. from("openjdk:8-jre")
  7. entryPoint(s"$targetDir/bin/${executableScriptName.value}")
  8. copy(appDir, targetDir, chown = "daemon:daemon")
  9. }
  10. }

Example with the sbt package task.

  1. docker / dockerfile := {
  2. val jarFile: File = (Compile / packageBin / sbt.Keys.`package`).value
  3. val classpath = (Compile / managedClasspath).value
  4. val mainclass = (Compile / packageBin / mainClass).value.getOrElse(sys.error("Expected exactly one main class"))
  5. val jarTarget = s"/app/${jarFile.getName}"
  6. // Make a colon separated classpath with the JAR file
  7. val classpathString = classpath.files.map("/app/" + _.getName)
  8. .mkString(":") + ":" + jarTarget
  9. new Dockerfile {
  10. // Base image
  11. from("openjdk:8-jre")
  12. // Add all files on the classpath
  13. add(classpath.files, "/app/")
  14. // Add the JAR file
  15. add(jarFile, jarTarget)
  16. // On launch run Java with the classpath and the main class
  17. entryPoint("java", "-cp", classpathString, mainclass)
  18. }
  19. }

Example with a Dockerfile in the filesystem.

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

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

Missing Dockerfile instructions

Dockerfile instructions that are missing from the sbt-docker DSL can still be used by calling the .customInstruction(instructionName, arguments) method.
Example:

  1. new Dockerfile {
  2. customInstruction("FROM", "openjdk AS stage1")
  3. run("build")
  4. customInstruction("FROM", "openjdk AS stage2")
  5. customInstruction("COPY", "--from=stage1 /path/to/file /path/to/file")
  6. customInstruction("STOPSIGNAL", "SIGQUIT")
  7. entryPoint("application")
  8. }

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 docker / imageNames 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 docker / imageNames key of type Seq[sbtdocker.ImageName].

Example:

  1. docker / imageNames := Seq(
  2. // Sets the latest tag
  3. ImageName(s"${organization.value}/${name.value}:latest"),
  4. // Sets a name with a tag that contains the project version
  5. ImageName(
  6. namespace = Some(organization.value),
  7. repository = name.value,
  8. tag = Some("v" + version.value)
  9. )
  10. )

Build options

Use the key docker / buildOptions to set build options.

cross-platform

The platforms parameter enables the cross-platform build.
With valuing this parameter the docker image will build using buildx command and the host environment should already been set up for.

⚠️ For using the cross builds you need QEMU binaries

  1. docker run --privileged --rm tonistiigi/binfmt --install all

Example:

  1. docker / buildOptions := BuildOptions(
  2. cache = false,
  3. removeIntermediateContainers = BuildOptions.Remove.Always,
  4. pullBaseImage = BuildOptions.Pull.Always,
  5. platforms = List("linux/arm64/v8"),
  6. additionalArguments = Seq("--add-host", "127.0.0.1:12345", "--compress")
  7. )

Build arguments

Use the key docker / dockerBuildArguments to set build arguments.

Example:

  1. docker / dockerBuildArguments := Map(
  2. "KEY" -> "value",
  3. "CREDENTIALS" -> sys.env("CREDENTIALS")
  4. )
  5. docker / dockerfile := {
  6. new Dockerfile {
  7. // ...
  8. arg("KEY")
  9. arg("CREDENTIALS")
  10. env("KEY" -> "$KEY", "CREDENTIALS" -> "$CREDENTIALS")
  11. // ...
  12. }
  13. }

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.