Skip to content

Commit

Permalink
Updated changes for Docker extension to sbt-native-packager
Browse files Browse the repository at this point in the history
- Trait DockerPlugin should not extend DockerKeys
- publishArtifact in Docker is false - do not generate POM/etc.
- Tasks for stage do not depend on each other.
- Change the CWD in the Docker container to install location.
- Adding Docker-specific mappings happens outside mapGenericFilesToDocker
- Use defaultLinuxInstallLocation instead of defaultDockerInstallLocation
- dockerBaseImage is set outside Docker config, easier to override
  • Loading branch information
Gary Coady committed May 10, 2014
1 parent 832d84a commit 6669278
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
28 changes: 13 additions & 15 deletions src/main/scala/com/typesafe/sbt/packager/docker/DockerPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@ import Keys._
import universal._
import sbt._

import sbt.Keys.cacheDirectory
import universal.Keys.stage

trait DockerPlugin extends Plugin with UniversalPlugin with DockerKeys {
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(
Cmd("FROM", dockerBaseImage),
Cmd("MAINTAINER", maintainer),
Cmd("ADD", "files /"),
Cmd("WORKDIR", "%s/bin" format dockerBaseDirectory),
ExecCmd("RUN", "chown", "-R", daemonUser, ".."),
Cmd("WORKDIR", "%s" format dockerBaseDirectory),
ExecCmd("RUN", "chown", "-R", daemonUser, "."),
Cmd("USER", daemonUser),
ExecCmd("ENTRYPOINT", name),
ExecCmd("ENTRYPOINT", "bin/%s" format name),

This comment has been minimized.

Copy link
@ahjohannessen

ahjohannessen May 24, 2014

Wouldn't it be better to have that as:

ExecCmd("ENTRYPOINT", "exec bin/%s" format name)

This comment has been minimized.

Copy link
@fiadliel

fiadliel May 30, 2014

Contributor

ExecCmd writes out the line in the format ["args", "to", "exec"]
So your suggestion would be written as
ENTRYPOINT ["exec bin/my-app"]

This would try to execute a command called "my-app" in a directory called "exec bin".

If, instead, this was in "Cmd" format, i.e.
ENTRYPOINT exec bin/%s
This would cause a shell to run which would then call exec. There appears to be little benefit in running a shell which immediately invokes exec, rather than docker calling exec itself.

This comment has been minimized.

Copy link
@ahjohannessen

ahjohannessen May 30, 2014

Ah, my bad. I was mostly thinking about an article I read about avoiding docker issuing a SIGKILL (http://www.tech-d.net/2014/01/27/docker-quicktip-2-exec-it/). But I think I mixed things up. It would be bash script that should use exec behaviour, if I understand it correctly. Which it does :) ->

execRunner () {
  # ...
  # we use "exec" here for our pids to be accurate.
  exec "$@"
}
ExecCmd("CMD")
).makeContent
}
Expand All @@ -42,32 +39,33 @@ trait DockerPlugin extends Plugin with UniversalPlugin with DockerKeys {
}

inConfig(Docker)(Seq(
mappings <<= (mappings in Universal, defaultDockerInstallLocation) map { (mappings, dest) =>
mappings <<= (mappings in Universal, defaultLinuxInstallLocation) map { (mappings, dest) =>
renameDests(mappings, dest)
},
mappings <++= dockerPackageMappings
}
))
}

def dockerSettings: Seq[Setting[_]] = Seq(
dockerBaseImage := "dockerfile/java",
sourceDirectory in Docker <<= sourceDirectory apply (_ / "docker"),
target in Docker <<= target apply (_ / "docker")
) ++ mapGenericFilesToDocker ++ inConfig(Docker)(Seq(
daemonUser := "daemon",
dockerBaseImage := "dockerfile/java",
defaultDockerInstallLocation := "/opt/docker",
dockerPackageMappings <<= (sourceDirectory in Docker) map { dir =>
publishArtifact := false,
defaultLinuxInstallLocation := "/opt/docker",
dockerPackageMappings <<= (sourceDirectory) map { dir =>
MappingsHelper contentOf dir
},
stage <<= dockerGenerateContext.dependsOn(dockerGenerateConfig),
mappings <++= dockerPackageMappings,
stage <<= (dockerGenerateConfig, dockerGenerateContext) map { (configFile, contextDir) => () },
dockerGenerateContext <<= (cacheDirectory, mappings, target) map {
(cacheDirectory, mappings, t) =>
val contextDir = t / "files"
stageFiles("docker")(cacheDirectory, contextDir, mappings)
contextDir
},
dockerGenerateConfig <<=
(dockerBaseImage, defaultDockerInstallLocation, maintainer in Docker, daemonUser in Docker, normalizedName in Docker, target in Docker) map {
(dockerBaseImage, defaultLinuxInstallLocation, maintainer, daemonUser, normalizedName, target) map {
case (dockerBaseImage, baseDirectory, maintainer, daemonUser, normalizedName, target) =>
generateDockerConfig(dockerBaseImage, baseDirectory, maintainer, daemonUser, normalizedName, target)
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/scala/com/typesafe/sbt/packager/docker/Keys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ import sbt._

trait DockerKeys {
val dockerGenerateConfig = TaskKey[File]("docker-generate-config", "Generates configuration file for Docker.")
val dockerGenerateContext = TaskKey[Unit]("docker-generate-context", "Generates context directory for Docker.")
val dockerGenerateContext = TaskKey[File]("docker-generate-context", "Generates context directory for Docker.")
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 defaultDockerInstallLocation = SettingKey[String]("defaultDockerInstallLocation", "The location where we will install Docker packages.")
}

object Keys extends DockerKeys {
def cacheDirectory = sbt.Keys.cacheDirectory
def mappings = sbt.Keys.mappings
def publishArtifact = sbt.Keys.publishArtifact
def sourceDirectory = sbt.Keys.sourceDirectory
def target = sbt.Keys.target
def defaultLinuxInstallLocation = packager.Keys.defaultLinuxInstallLocation
def normalizedName = universal.Keys.normalizedName
def stage = universal.Keys.stage
def daemonUser = linux.Keys.daemonUser
def maintainer = linux.Keys.maintainer
}

0 comments on commit 6669278

Please sign in to comment.