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

Separate Docker Layers for Dependencies and App jars. #1310

Merged
merged 21 commits into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
59 changes: 33 additions & 26 deletions src/main/scala/com/typesafe/sbt/packager/docker/DockerPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.util.UUID
import java.util.concurrent.atomic.AtomicBoolean

import sbt._
import sbt.Keys.{clean, mappings, name, publish, publishLocal, sourceDirectory, streams, target, version}
import sbt.Keys.{clean, mappings, name, organization, publish, publishLocal, sourceDirectory, streams, target, version}
import com.typesafe.sbt.packager.Keys._
import com.typesafe.sbt.packager.linux.LinuxPlugin.autoImport.{daemonUser, defaultLinuxInstallLocation}
import com.typesafe.sbt.packager.universal.UniversalPlugin
Expand Down Expand Up @@ -98,6 +98,12 @@ object DockerPlugin extends AutoPlugin {
),
dockerUpdateLatest := false,
dockerAutoremoveMultiStageIntermediateImages := true,
dockerLayerGrouping := {
case s if s.startsWith(s"lib/${organization.value}") => 2
case s if s.startsWith(s"bin/") => 1
case s if s.startsWith(s"lib/") => 0
case _ => 0
},
dockerAliases := {
val alias = dockerAlias.value
if (dockerUpdateLatest.value) {
Expand Down Expand Up @@ -142,7 +148,6 @@ object DockerPlugin extends AutoPlugin {
val base = dockerBaseImage.value
val addPerms = dockerAdditionalPermissions.value
val multiStageId = UUID.randomUUID().toString

val generalCommands = makeFrom(base) +: makeMaintainer((maintainer in Docker).value).toSeq
val stage0name = "stage0"
val stage0: Seq[CmdLike] = strategy match {
Expand All @@ -169,7 +174,10 @@ object DockerPlugin extends AutoPlugin {
Seq(makeWorkdir(dockerBaseDirectory)) ++
(strategy match {
case DockerPermissionStrategy.MultiStage =>
Seq(makeCopyFrom(dockerBaseDirectory, stage0name, user, group))
val layerIdsAscending = (dockerLayerMappings in Docker).value.map(_._1).distinct.sorted(Ordering.Int)
layerIdsAscending.map { layerId =>
makeCopyFrom(dockerBaseDirectory + "/" + layerId, dockerBaseDirectory, stage0name, user, group)
}
case DockerPermissionStrategy.Run =>
Seq(makeCopy(dockerBaseDirectory), makeChmodRecursive(dockerChmodType.value, Seq(dockerBaseDirectory))) ++
(addPerms map { case (tpe, v) => makeChmod(tpe, Seq(v)) })
Expand All @@ -192,10 +200,12 @@ object DockerPlugin extends AutoPlugin {

stage0 ++ stage1
}
) ++ mapGenericFilesToDocker ++ inConfig(Docker)(
) ++ inConfig(Docker)(
Seq(
executableScriptName := executableScriptName.value,
mappings ++= dockerPackageMappings.value,
mappings := dockerLayerMappings.value.map {
case (_, file, path) => (file, path)
},
name := name.value,
packageName := packageName.value,
publishLocal := {
Expand Down Expand Up @@ -234,6 +244,18 @@ object DockerPlugin extends AutoPlugin {
stage := Stager.stage(Docker.name)(streams.value, stagingDirectory.value, mappings.value),
stage := (stage dependsOn dockerGenerateConfig).value,
stagingDirectory := (target in Docker).value / "stage",
dockerLayerMappings := {
val dockerGroups = dockerLayerGrouping.value
def buildDockerPaths(seq: Seq[(File, String)], prefix: String) =
for {
(file, path) <- seq
layerIdx = dockerGroups(path)
pathInDocker = s"$prefix/$layerIdx/$path"
} yield (layerIdx, file, pathInDocker)

buildDockerPaths((mappings in Universal).value, (defaultLinuxInstallLocation in Docker).value + "/") ++
buildDockerPaths(dockerPackageMappings.value, "")
},
target := target.value / "docker",
// pick a user name that's unlikely to exist in base images
daemonUser := "demiourgos728",
Expand Down Expand Up @@ -328,21 +350,21 @@ object DockerPlugin extends AutoPlugin {
}

/**
* @param dockerBaseDirectory the installation directory
* @param from files are copied from the given build stage
* @param src the installation directory
* @param stage files are copied from the given build stage
* @param daemonUser
* @param daemonGroup
* @return COPY command copying all files inside the directory from another build stage.
*/
private final def makeCopyFrom(dockerBaseDirectory: String,
from: String,
private final def makeCopyFrom(src: String,
dest: String,
stage: String,
daemonUser: String,
daemonGroup: String): CmdLike =
Cmd("COPY", s"--from=$from --chown=$daemonUser:$daemonGroup $dockerBaseDirectory $dockerBaseDirectory")
Cmd("COPY", s"--from=$stage --chown=$daemonUser:$daemonGroup $src $dest")

/**
* @param dockerBaseDirectory the installation directory
* @param from files are copied from the given build stage
* @param daemonUser
* @param daemonGroup
* @return COPY command copying all files inside the directory from another build stage.
Expand Down Expand Up @@ -487,20 +509,6 @@ object DockerPlugin extends AutoPlugin {
f
}

/**
* uses the `mappings in Universal` to generate the
* `mappings in Docker`.
*/
def mapGenericFilesToDocker: Seq[Setting[_]] = {
def renameDests(from: Seq[(File, String)], dest: String) =
for {
(f, path) <- from
newPath = "%s/%s" format (dest, path)
} yield (f, newPath)

inConfig(Docker)(Seq(mappings := renameDests((mappings in Universal).value, defaultLinuxInstallLocation.value)))
}

private[packager] def publishLocalLogger(log: Logger) =
new sys.process.ProcessLogger {
override def err(err: => String): Unit =
Expand Down Expand Up @@ -716,5 +724,4 @@ object DockerPlugin extends AutoPlugin {
case _ => List.empty
}
}

}
6 changes: 6 additions & 0 deletions src/main/scala/com/typesafe/sbt/packager/docker/Keys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,10 @@ private[packager] trait DockerKeysEx extends DockerKeys {
lazy val dockerAdditionalPermissions =
taskKey[Seq[(DockerChmodType, String)]]("Explicit chmod calls to some of the paths.")
val dockerApiVersion = TaskKey[Option[DockerApiVersion]]("dockerApiVersion", "The docker server api version")
val dockerLayerGrouping = settingKey[String => Int](
"Group files by path into in layers to increase docker cache hits. " +
"Lower index means the file would be a part of an earlier layer."
)
val dockerLayerMappings =
taskKey[Seq[(Int, File, String)]]("List of layer, source file and destination in Docker image.")
}