Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docker] add dockerEnvVars support #1137

Merged
merged 7 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object DockerPlugin extends AutoPlugin {
*/
val UnixSeparatorChar = '/'

override def requires = UniversalPlugin
override def requires: Plugins = UniversalPlugin

override def projectConfigurations: Seq[Configuration] = Seq(Docker)

Expand All @@ -71,6 +71,7 @@ object DockerPlugin extends AutoPlugin {
dockerExposedUdpPorts := Seq(),
dockerExposedVolumes := Seq(),
dockerLabels := Map(),
dockerEnvVars := Map(),
dockerRepository := None,
dockerUsername := None,
dockerAlias := DockerAlias(
Expand Down Expand Up @@ -104,6 +105,7 @@ object DockerPlugin extends AutoPlugin {
generalCommands ++
Seq(makeWorkdir(dockerBaseDirectory)) ++ makeAdd(dockerVersion.value, dockerBaseDirectory, user, group) ++
dockerLabels.value.map(makeLabel) ++
dockerEnvVars.value.map(makeEnvVar) ++
makeExposePorts(dockerExposedPorts.value, dockerExposedUdpPorts.value) ++
makeVolumes(dockerExposedVolumes.value, user, group) ++
Seq(makeUser(user), makeEntrypoint(dockerEntrypoint.value), makeCmd(dockerCmd.value))
Expand Down Expand Up @@ -175,6 +177,15 @@ object DockerPlugin extends AutoPlugin {
Cmd("LABEL", variable + "=\"" + value.toString + "\"")
}

/**
* @param envVar
* @return ENV command
*/
private final def makeEnvVar(envVar: (String, String)): CmdLike = {
val (variable, value) = envVar
Cmd("ENV", variable + "=\"" + value.toString + "\"")
}

/**
* @param dockerBaseDirectory, the installation directory
*/
Expand Down
2 changes: 2 additions & 0 deletions src/main/scala/com/typesafe/sbt/packager/docker/Keys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ trait DockerKeys {
val dockerBuildOptions = SettingKey[Seq[String]]("dockerBuildOptions", "Options used for the Docker build")
val dockerBuildCommand = SettingKey[Seq[String]]("dockerBuildCommand", "Command for building the Docker image")
val dockerLabels = SettingKey[Map[String, String]]("dockerLabels", "Labels applied to the Docker image")
val dockerEnvVars =
SettingKey[Map[String, String]]("dockerEnvVars", "Environment Variables applied to the Docker image")
val dockerRmiCommand =
SettingKey[Seq[String]]("dockerRmiCommand", "Command for removing the Docker image from the local registry")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ trait CmdLike {
* }}}
*/
case class ExecCmd(cmd: String, args: String*) extends CmdLike {
def makeContent =
def makeContent: String =
"%s [%s]\n" format (cmd, args.map('"' + _ + '"').mkString(", "))
}

Expand All @@ -59,7 +59,7 @@ case class ExecCmd(cmd: String, args: String*) extends CmdLike {
* }}}
*/
case class Cmd(cmd: String, args: String*) extends CmdLike {
def makeContent = "%s %s\n" format (cmd, args.mkString(" "))
def makeContent: String = "%s %s\n" format (cmd, args.mkString(" "))
}

/**
Expand All @@ -76,7 +76,7 @@ case class Cmd(cmd: String, args: String*) extends CmdLike {
* }}}
*/
case class CombinedCmd(cmd: String, arg: CmdLike) extends CmdLike {
def makeContent = "%s %s\n" format (cmd, arg.makeContent)
def makeContent: String = "%s %s\n" format (cmd, arg.makeContent)
}

/** Represents dockerfile used by docker when constructing packages. */
Expand Down
19 changes: 19 additions & 0 deletions src/sbt-test/docker/envVars/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
enablePlugins(DockerPlugin)

name := "simple-test"

version := "0.1.0"

dockerEnvVars := Map("FOO" -> "bar", "FOO_BAR" -> "foo bar", "NUMBER" -> "123")


TaskKey[Unit]("checkDockerfile") := {
val dockerfile = IO.read((stagingDirectory in Docker).value / "Dockerfile")

assert(dockerfile contains """ENV FOO="bar"""", s"does not contain foo=bar\n$dockerfile")
assert(dockerfile contains """ENV FOO_BAR="foo bar"""", s"does not contain foo=bar\n$dockerfile")
assert(dockerfile contains """ENV NUMBER="123"""", s"does not contain foo=bar\n$dockerfile")

streams.value.log.success("Successfully tested Dockerfile")
()
}
1 change: 1 addition & 0 deletions src/sbt-test/docker/envVars/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % sys.props("project.version"))
3 changes: 3 additions & 0 deletions src/sbt-test/docker/envVars/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Stage the distribution and ensure files show up.
> docker:stage
> checkDockerfile
3 changes: 3 additions & 0 deletions src/sphinx/formats/docker.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ Environment Settings
``dockerLabels``
A map of labels that will be applied to the Docker image.

``dockerEnvVars``
A map of environment variables that will be applied to the Docker image.

``dockerEntrypoint``
Overrides the default entrypoint for docker-specific service discovery tasks before running the application.
Defaults to the bash executable script, available at ``bin/<script name>`` in the current ``WORKDIR`` of ``/opt/docker``.
Expand Down