sbt-docker icon indicating copy to clipboard operation
sbt-docker copied to clipboard

Question: Best approach to generating more than one dockerfile from a module?

Open scottcarey opened this issue 8 years ago • 3 comments

I have a requirement to build two dockerfiles from one module. These will have different base images and various configurations.

The plugin seems to assume a singular docker artifact per module. I can do some messy stuff in sbt... that gets more and more messy by making my own set of configuration keys that mirror everything the plugin does, clones of 'docker' 'dockerBuildAndPush' etc.

That is a horrible way to go about it. Is there a better way?

scottcarey avatar Aug 01 '17 21:08 scottcarey

I'm interested in the same question.

ILLKO avatar Aug 09 '17 10:08 ILLKO

I've came up with following approach. Not ideal, but gets the job done:

dockerfile in docker := {
  if (sys.env.get("SOME_VALUE").isDefined) {
    new Dockerfile {
      … // some docker file
    }
  } else {
    new Dockerfile {
      … // alternative docker file
    }
  }
}

Basically, in sbt you can have arbitrary code inside the task definition body. You can access environment, read settings, etc. Hope this helps.

P.S. A better way would be not to scope sbt-docker's settings and values under docker task key, but rather let them be scoped in natural sbt way. Then one could've defined e.g. custom sbt configurations to scope dockerfile, imageNames, etc.

RomanIakovlev avatar Nov 17 '17 15:11 RomanIakovlev

This could also work:

  import sbtdocker.DockerSettings

  lazy val OtherArtifact = config("otherArtifact")

  .settings(
    inConfig(OtherArtifact)(DockerSettings.baseDockerSettings),
    OtherArtifact / docker / imageNames := ???, // alternative image name
    OtherArtifact / docker / dockerfile := ??? // alternative docker file
  )

sbt OtherArtifact/docker should build alternative image whereas sbt docker should build primary one.

argast avatar Sep 13 '22 11:09 argast