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

Add optional network setting #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -15,4 +15,5 @@ object DockerCommandLineOptions {
val memory = "--memory"
val volumes = "--volumes"
val environment = "--env"
val network = "--network"
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ trait DockerContainerKeys {
lazy val startLocalDockerContainer = TaskKey[Unit]("startLocal", "Start the Docker container. Depends on createLocal to create the container to be started.") in Docker

lazy val dockerContainerMemoryLimit = settingKey[Option[String]]("memory limit for created Docker container. e.g., Option('192M')")
lazy val dockerContainerNetwork = settingKey[Option[String]]("Connect a container to a private network")

lazy val dockerContainerPortPublishing = settingKey[Map[Int, Option[Int]]]("Docker container:host port mappings")
lazy val dockerContainerPublishAllPorts = settingKey[Boolean]("Auto-map all exposed ports")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ object DockerContainerPlugin extends AutoPlugin {
dockerContainerPublishAllPorts := false,
dockerContainerPortPublishing := Map.empty[Int, Option[Int]],
dockerContainerLinks := Map.empty[String, String],
dockerContainerAdditionalEnvironmentVariables := Map.empty[String, Option[String]]
dockerContainerAdditionalEnvironmentVariables := Map.empty[String, Option[String]],
dockerContainerNetwork := None
)

lazy val dockerCreateArguments = TaskKey[DockerCreateArguments]("dockerCreateArguments", "task key used internally for testing the createLocal task") in Docker
Expand All @@ -38,7 +39,9 @@ object DockerContainerPlugin extends AutoPlugin {
dockerContainerPortPublishing.value,
dockerContainerPublishAllPorts.value,
dockerContainerLinks.value,
dockerContainerAdditionalEnvironmentVariables.value),
dockerContainerAdditionalEnvironmentVariables.value,
dockerContainerNetwork.value
),
createLocalDockerContainer := runDockerCreateAndReturnContainerName(dockerCreateArguments.value, streams.value.log, (publishLocal in Docker).value),

dockerStartArguments := DockerStartArguments(createLocalDockerContainer.value),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ case class DockerCreateArguments(containerName: ContainerName,
publishedPorts: PublishedPorts,
autoPublishAllPorts: Boolean,
linkedContainers: LinkedContainers,
environment: Environment)
environment: Environment,
network: Option[NetworkName])
extends DockerProcessBuilder {

override def argumentSequence: Seq[String] = (Some(DockerCommandLineOptions.dockerCreate) ++
Expand All @@ -21,11 +22,14 @@ case class DockerCreateArguments(containerName: ContainerName,
).flatMap(_.reified) ++
publishAllExposedPorts ++
memoryLimitToDockerCommand ++
networkToDockerCommand ++
Some(imageName)).toSeq

private def publishAllExposedPorts: Option[String] = if (autoPublishAllPorts) Some(publishAllPorts) else None

private def memoryLimitToDockerCommand = memoryLimit.map(_.reified).getOrElse(missing)

private def networkToDockerCommand = network.map(_.reified).getOrElse(missing)
}

object DockerCreateArguments {
Expand All @@ -36,15 +40,17 @@ object DockerCreateArguments {
portPublishing: Map[Int, Option[Int]],
publishAllPorts: Boolean,
links: Map[String, String],
env: Map[String, Option[String]]): DockerCreateArguments =
env: Map[String, Option[String]],
network: Option[String]): DockerCreateArguments =
DockerCreateArguments(
ContainerName(containerName),
imageName,
memoryLimit.map(MemoryLimit),
PublishedPorts(portPublishing.toSeq: _*),
publishAllPorts,
LinkedContainers(links.toSeq: _*),
Environment(env.toSeq: _*)
Environment(env.toSeq: _*),
network.map(NetworkName)
)

private val missing = Seq.empty[String]
Expand All @@ -69,6 +75,10 @@ object DockerCreateArguments {
override def reified: Seq[String] = Seq(memory, limit)
}

case class NetworkName(name: String) extends Argument {
override def reified: Seq[String] = Seq(network, name)
}

case class PublishedPorts(publishedPorts: (Int, Option[Int])*) extends MappingArgument {
val argumentLabel = publishPort

Expand All @@ -87,6 +97,8 @@ object DockerCreateArguments {
}
}



case class Environment(env: (String, Option[String])*) extends MappingArgument {
override lazy val definedMapping: Seq[String] = env.map {
case (key, maybeValue) ⇒
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class DockerCreateArgumentsSpec extends Specification with AdditionalSeqMatchers
publishedPorts = PublishedPorts(77 → DockerContainerPlugin.autoImport.AutoAssign, 1983 → Option(42)),
autoPublishAllPorts = true,
linkedContainers = LinkedContainers("a" → "b"),
environment = Environment("env" → Option("value"), "PASS_THROUGH" → DockerContainerPlugin.autoImport.Passthru)
environment = Environment("env" → Option("value"), "PASS_THROUGH" → DockerContainerPlugin.autoImport.Passthru),
network = Some(NetworkName("testnetwork"))
)

val output = input.argumentSequence
Expand All @@ -35,6 +36,7 @@ class DockerCreateArgumentsSpec extends Specification with AdditionalSeqMatchers
output must containSlice("--link", "a:b")
output must containSlice("--env", "env=value")
output must containSlice("--env", "PASS_THROUGH")
output must containSlice("--network", "testnetwork")
}

"convert SBT types to case class" in new Setup {
Expand All @@ -45,7 +47,8 @@ class DockerCreateArgumentsSpec extends Specification with AdditionalSeqMatchers
publishedPorts = PublishedPorts(77 → DockerContainerPlugin.autoImport.AutoAssign, 1983 → Option(42)),
autoPublishAllPorts = true,
linkedContainers = LinkedContainers("a" → "b"),
environment = Environment("env" → Option("value"), "PASS_THROUGH" → DockerContainerPlugin.autoImport.Passthru)
environment = Environment("env" → Option("value"), "PASS_THROUGH" → DockerContainerPlugin.autoImport.Passthru),
network = Some(NetworkName("testnetwork"))
)

val output = DockerCreateArguments.fromBasicSbtTypes(
Expand All @@ -55,7 +58,8 @@ class DockerCreateArgumentsSpec extends Specification with AdditionalSeqMatchers
portPublishing = Map(77 → DockerContainerPlugin.autoImport.AutoAssign, 1983 → Option(42)),
publishAllPorts = true,
links = Map("a" → "b"),
env = Map("env" → Option("value"), "PASS_THROUGH" → DockerContainerPlugin.autoImport.Passthru)
env = Map("env" → Option("value"), "PASS_THROUGH" → DockerContainerPlugin.autoImport.Passthru),
network = Some("testnetwork")
)

output must_== expected
Expand Down