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

Support EXPOSE [port] ... for Docker #278

Merged
merged 1 commit into from
Jun 23, 2014
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
26 changes: 18 additions & 8 deletions src/main/scala/com/typesafe/sbt/packager/docker/DockerPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 /"),
Expand All @@ -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)
Expand Down Expand Up @@ -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
},
Expand All @@ -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)
}
))
}
1 change: 1 addition & 0 deletions src/main/scala/com/typesafe/sbt/packager/docker/Keys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down