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 Docker volumes. #280

Merged
merged 2 commits 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
30 changes: 23 additions & 7 deletions src/main/scala/com/typesafe/sbt/packager/docker/DockerPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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, exposedPorts: Seq[Int]) = {
private[this] final def makeDockerContent(dockerBaseImage: String, dockerBaseDirectory: String, maintainer: String, daemonUser: String, name: String, exposedPorts: Seq[Int], exposedVolumes: Seq[String]) = {
val dockerCommands = Seq(
Cmd("FROM", dockerBaseImage),
Cmd("MAINTAINER", maintainer),
Expand All @@ -27,12 +27,27 @@ trait DockerPlugin extends Plugin with UniversalPlugin {
Some(Cmd("EXPOSE", exposedPorts.mkString(" ")))
}

Dockerfile(dockerCommands ++ exposeCommand: _*).makeContent
// If the exposed volume does not exist, the volume is made available
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for documenting these edge cases! Keep up the good work.

// with root ownership. This may be too strict for some directories,
// and we lose the feature that all directories below the install path
// can be written to by the binary. Therefore the directories are
// created before the ownership is changed.
val volumeCommands: Seq[CmdLike] = {
if (exposedVolumes.isEmpty)
Seq()
else
Seq(
ExecCmd("RUN", Seq("mkdir", "-p") ++ exposedVolumes: _*),
ExecCmd("VOLUME", exposedVolumes: _*)
)
}

Dockerfile(volumeCommands ++ exposeCommand ++ dockerCommands: _*).makeContent
}

private[this] final def generateDockerConfig(
dockerBaseImage: String, dockerBaseDirectory: String, maintainer: String, daemonUser: String, normalizedName: String, exposedPorts: Seq[Int], target: File) = {
val dockerContent = makeDockerContent(dockerBaseImage, dockerBaseDirectory, maintainer, daemonUser, normalizedName, exposedPorts)
dockerBaseImage: String, dockerBaseDirectory: String, maintainer: String, daemonUser: String, normalizedName: String, exposedPorts: Seq[Int], exposedVolumes: Seq[String], target: File) = {
val dockerContent = makeDockerContent(dockerBaseImage, dockerBaseDirectory, maintainer, daemonUser, normalizedName, exposedPorts, exposedVolumes)

val f = target / "Dockerfile"
IO.write(f, dockerContent)
Expand Down Expand Up @@ -63,6 +78,7 @@ trait DockerPlugin extends Plugin with UniversalPlugin {
publishArtifact := false,
defaultLinuxInstallLocation := "/opt/docker",
dockerExposedPorts := Seq(),
dockerExposedVolumes := Seq(),
dockerPackageMappings <<= (sourceDirectory) map { dir =>
MappingsHelper contentOf dir
},
Expand All @@ -75,9 +91,9 @@ trait DockerPlugin extends Plugin with UniversalPlugin {
contextDir
},
dockerGenerateConfig <<=
(dockerBaseImage, defaultLinuxInstallLocation, maintainer, daemonUser, normalizedName, dockerExposedPorts, target) map {
case (dockerBaseImage, baseDirectory, maintainer, daemonUser, normalizedName, exposedPorts, target) =>
generateDockerConfig(dockerBaseImage, baseDirectory, maintainer, daemonUser, normalizedName, exposedPorts, target)
(dockerBaseImage, defaultLinuxInstallLocation, maintainer, daemonUser, normalizedName, dockerExposedPorts, dockerExposedVolumes, target) map {
case (dockerBaseImage, baseDirectory, maintainer, daemonUser, normalizedName, exposedPorts, exposedVolumes, target) =>
generateDockerConfig(dockerBaseImage, baseDirectory, maintainer, daemonUser, normalizedName, exposedPorts, exposedVolumes, 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 @@ -11,6 +11,7 @@ trait DockerKeys {

val dockerBaseImage = SettingKey[String]("dockerBaseImage", "Base image for Dockerfile.")
val dockerExposedPorts = SettingKey[Seq[Int]]("dockerExposedPorts", "Ports exposed by Docker image")
val dockerExposedVolumes = SettingKey[Seq[String]]("dockerExposedVolumes", "Volumes exposed by Docker image")
}

object Keys extends DockerKeys {
Expand Down
9 changes: 9 additions & 0 deletions src/sbt-test/docker/volumes/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import NativePackagerKeys._

packagerSettings

name := "simple-test"

version := "0.1.0"

dockerExposedVolumes in Docker := Seq("/opt/docker/logs", "/opt/docker/config")
1 change: 1 addition & 0 deletions src/sbt-test/docker/volumes/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"))
4 changes: 4 additions & 0 deletions src/sbt-test/docker/volumes/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Stage the distribution and ensure files show up.
> docker:stage
$ exec grep -q -F 'VOLUME ["/opt/docker/logs", "/opt/docker/config"]' target/docker/Dockerfile
$ exec grep -q -F 'RUN ["mkdir", "-p", "/opt/docker/logs", "/opt/docker/config"]' target/docker/Dockerfile