From 6dde86fbdca07cd4c306a85e1d48532ca99a20bf Mon Sep 17 00:00:00 2001 From: Gary Coady Date: Sun, 22 Jun 2014 18:49:45 +0100 Subject: [PATCH] Support EXPOSE [port] ... for Docker Docker allows the ability to share ports between containers, if the ports are listed in the configuration for the Docker image. This commit adds a dockerExposedPorts setting which adds the appropriate line. --- .../sbt/packager/docker/DockerPlugin.scala | 26 +++++++++++++------ .../typesafe/sbt/packager/docker/Keys.scala | 1 + 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main/scala/com/typesafe/sbt/packager/docker/DockerPlugin.scala b/src/main/scala/com/typesafe/sbt/packager/docker/DockerPlugin.scala index bbd0827a1..3ac806b00 100644 --- a/src/main/scala/com/typesafe/sbt/packager/docker/DockerPlugin.scala +++ b/src/main/scala/com/typesafe/sbt/packager/docker/DockerPlugin.scala @@ -8,8 +8,8 @@ import sbt._ trait DockerPlugin extends Plugin with UniversalPlugin { val Docker = config("docker") extend Universal - private[this] final def makeDockerContent(dockerBaseImage: String, dockerBaseDirectory: String, maintainer: String, daemonUser: String, name: String) = { - Dockerfile( + private[this] final def makeDockerContent(dockerBaseImage: String, dockerBaseDirectory: String, maintainer: String, daemonUser: String, name: String, exposedPorts: Seq[Int]) = { + val dockerCommands = Seq( Cmd("FROM", dockerBaseImage), Cmd("MAINTAINER", maintainer), Cmd("ADD", "files /"), @@ -18,12 +18,21 @@ trait DockerPlugin extends Plugin with UniversalPlugin { Cmd("USER", daemonUser), ExecCmd("ENTRYPOINT", "bin/%s" format name), ExecCmd("CMD") - ).makeContent + ) + + val exposeCommand: Option[CmdLike] = { + if (exposedPorts.isEmpty) + None + else + Some(Cmd("EXPOSE", exposedPorts.mkString(" "))) + } + + Dockerfile(dockerCommands ++ exposeCommand: _*).makeContent } private[this] final def generateDockerConfig( - dockerBaseImage: String, dockerBaseDirectory: String, maintainer: String, daemonUser: String, normalizedName: String, target: File) = { - val dockerContent = makeDockerContent(dockerBaseImage, dockerBaseDirectory, maintainer, daemonUser, normalizedName) + dockerBaseImage: String, dockerBaseDirectory: String, maintainer: String, daemonUser: String, normalizedName: String, exposedPorts: Seq[Int], target: File) = { + val dockerContent = makeDockerContent(dockerBaseImage, dockerBaseDirectory, maintainer, daemonUser, normalizedName, exposedPorts) val f = target / "Dockerfile" IO.write(f, dockerContent) @@ -53,6 +62,7 @@ trait DockerPlugin extends Plugin with UniversalPlugin { daemonUser := "daemon", publishArtifact := false, defaultLinuxInstallLocation := "/opt/docker", + dockerExposedPorts := Seq(), dockerPackageMappings <<= (sourceDirectory) map { dir => MappingsHelper contentOf dir }, @@ -65,9 +75,9 @@ trait DockerPlugin extends Plugin with UniversalPlugin { contextDir }, dockerGenerateConfig <<= - (dockerBaseImage, defaultLinuxInstallLocation, maintainer, daemonUser, normalizedName, target) map { - case (dockerBaseImage, baseDirectory, maintainer, daemonUser, normalizedName, target) => - generateDockerConfig(dockerBaseImage, baseDirectory, maintainer, daemonUser, normalizedName, target) + (dockerBaseImage, defaultLinuxInstallLocation, maintainer, daemonUser, normalizedName, dockerExposedPorts, target) map { + case (dockerBaseImage, baseDirectory, maintainer, daemonUser, normalizedName, exposedPorts, target) => + generateDockerConfig(dockerBaseImage, baseDirectory, maintainer, daemonUser, normalizedName, exposedPorts, target) } )) } diff --git a/src/main/scala/com/typesafe/sbt/packager/docker/Keys.scala b/src/main/scala/com/typesafe/sbt/packager/docker/Keys.scala index 5665e1fd6..ea6eb838f 100644 --- a/src/main/scala/com/typesafe/sbt/packager/docker/Keys.scala +++ b/src/main/scala/com/typesafe/sbt/packager/docker/Keys.scala @@ -10,6 +10,7 @@ trait DockerKeys { val dockerPackageMappings = TaskKey[Seq[(File, String)]]("docker-package-mappings", "Generates location mappings for Docker build.") val dockerBaseImage = SettingKey[String]("dockerBaseImage", "Base image for Dockerfile.") + val dockerExposedPorts = SettingKey[Seq[Int]]("dockerExposedPorts", "Ports exposed by Docker image") } object Keys extends DockerKeys {