regis-leray / sbt-docker

Create Docker images directly from Dockerfile

GitHub

sbt-docker

sbt-docker is an sbt plugin that builds and pushes Docker images by using Dockerfile.

Requirements

  • Sbt
  • Docker

Setup

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

addSbtPlugin("com.github.regis-leray" % "sbt-docker" % "0.4.0")

in your build.sbt need to activate manually the plugin for each project

lazy val root = project.in(file("."))
  .enablePlugins(DockerPlugin)

Don't forget to export your DOCKER_ID_USER or DOCKER_TAG_NAMESPACE as environment variable, to be able to build and push docker image You can override dockerTagNamespace to provide it

https://docs.docker.com/docker-hub/repos/#searching-for-images

https://docs.docker.com/docker-cloud/builds/push-images/

sbt-docker is an auto plugin, this means that sbt version 0.13.5 or higher is required.

Building an image

To build an image use the dockerBuild task.

Simply run sbt dockerBuild from your prompt or dockerBuild in the sbt console.

The Dockerfile should be located at the base directory of you project ./Dockerfile

// Example if you need to override keys

lazy val root = project.in(file("."))
  .settings(
      //override default docker image name :: default `$name`
      dockerImageName := "hello",
      //override default docker image version :: default `$version`
      dockerImageVersion := "1.0-RC",
      //override default dockerTagNamespace :: default `sys.env.get("DOCKER_ID_USER") or sys.env.get("DOCKER_TAG_NAMESPACE")`     
      dockerTagNamespace := "mycompany",
      //override default tag  :: default `$dockerTagNamespace/$dockerImageName:$dockerImageVersion`
      dockerTag := "quai.io/mycompany/hello:1.0",     
      //provide build OPTIONS :: default `Nil`
      dockerBuildOptions := Seq("--no-cache"),
      //default location of dockerfile :: default `baseDirectory`
      dockerContextPath := baseDirectory.value.asPath.resolve("docker-dir"),
      //default name of dockerfile :: default `Dockerfile`
      dockerfileName := "Dockerfile-custom"
   )
  .enablePlugins(DockerPlugin)

if you need to support quai.io (private repo) only override dockerTagNamespace

lazy val root = project.in(file(".")) 
 .settings(dockerTagNamespace := Some("quai.io/mycompany"))
 .enablePlugins(DockerPlugin)

How to create and push docker image on quai.io

https://docs.quay.io/guides/create-repo.html

Pushing an image

An image that have already been built can be pushed with the dockerPush task.

To push an image use the dockerPush task.

By default dockerPush will push your docker image build to dockerhub

dockerTag key is used to determine which image names to push.

// Example if you need to override keys

lazy val root = project.in(file("."))
  .settings(
      //provide push OPTIONS :: default `Nil`
      dockerPushOptions := Seq("--disable-content-trust"),
      //override default tag `$dockerIdUserName/$name:$version`
      dockerTag := "quai.io/mycompany/hello:1.0", 
   )
  .enablePlugins(DockerPlugin)

You need to be authenticated by using docker login -u $DOCKER_ID_USER, if not you can't push docker images

https://docs.docker.com/docker-cloud/builds/push-images/

Building & Pushing an image

To build and push an image use the dockerBuildAndPush task.