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 5 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
94 changes: 62 additions & 32 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,20 @@ object DockerPlugin extends AutoPlugin {
),
dockerUpdateLatest := false,
dockerAutoremoveMultiStageIntermediateImages := true,
dockerLayerGrouping := {
val dockerBaseDirectory = (defaultLinuxInstallLocation in Docker).value
(path: String) =>
{
val pathInWorkdir = path.stripPrefix(dockerBaseDirectory)
if (pathInWorkdir.startsWith(s"/lib/${organization.value}"))
2
else if (pathInWorkdir.startsWith("/lib/"))
1
else if (pathInWorkdir.startsWith("/bin/"))
1
else 0
}
},
dockerAliases := {
val alias = dockerAlias.value
if (dockerUpdateLatest.value) {
Expand All @@ -123,13 +137,13 @@ object DockerPlugin extends AutoPlugin {
dockerBuildCommand := dockerExecCommand.value ++ Seq("build") ++ dockerBuildOptions.value ++ Seq("."),
dockerAdditionalPermissions := {
val basePath = (defaultLinuxInstallLocation in Docker).value
(mappings in Docker).value
(dockerLayerMappings in Docker).value
.collect {
// by default we assume everything in the bin/ folder should be executable that is not a .bat file
case (_, path) if path.startsWith(s"$basePath/bin/") && !path.endsWith(".bat") =>
case (_, _, path) if path.contains(s"$basePath/bin/") && !path.endsWith(".bat") =>
DockerChmodType.UserGroupPlusExecute -> path
// sh files should also be marked as executable
case (_, path) if path.endsWith(".sh") => DockerChmodType.UserGroupPlusExecute -> path
case (_, _, path) if path.endsWith(".sh") => DockerChmodType.UserGroupPlusExecute -> path
}
},
dockerCommands := {
Expand All @@ -142,20 +156,19 @@ 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 layerIdsAscending = (dockerLayerMappings in Docker).value.map(_._1).distinct.sorted
val stage0: Seq[CmdLike] = strategy match {
case DockerPermissionStrategy.MultiStage =>
Seq(
makeFromAs(base, stage0name),
makeLabel("snp-multi-stage" -> "intermediate"),
makeLabel("snp-multi-stage-id" -> multiStageId),
makeWorkdir(dockerBaseDirectory),
makeCopy(dockerBaseDirectory),
makeUser("root"),
makeChmodRecursive(dockerChmodType.value, Seq(dockerBaseDirectory))
makeWorkdir(dockerBaseDirectory)
) ++
layerIdsAscending.map(l => makeCopy(s"$l", s"/$l/")) ++
Seq(makeUser("root"), makeChmodRecursive(dockerChmodType.value, Seq(dockerBaseDirectory))) ++
(addPerms map { case (tpe, v) => makeChmod(tpe, Seq(v)) }) ++
Seq(DockerStageBreak)
case _ => Seq()
Expand All @@ -169,7 +182,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
layerIdsAscending.map { layerId =>
makeCopyFrom(s"/$layerId$dockerBaseDirectory", 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,9 +208,10 @@ object DockerPlugin extends AutoPlugin {

stage0 ++ stage1
}
) ++ mapGenericFilesToDocker ++ inConfig(Docker)(
) ++ inConfig(Docker)(
Seq(
executableScriptName := executableScriptName.value,
mappings := mappingsInDocker((mappings in Universal).value, defaultLinuxInstallLocation.value),
mappings ++= dockerPackageMappings.value,
name := name.value,
packageName := packageName.value,
Expand Down Expand Up @@ -231,9 +248,21 @@ object DockerPlugin extends AutoPlugin {
}
},
sourceDirectory := sourceDirectory.value / "docker",
stage := Stager.stage(Docker.name)(streams.value, stagingDirectory.value, mappings.value),
stage := Stager.stage(Docker.name)(streams.value, stagingDirectory.value, dockerLayerMappings.value.map {
case (_, file, path) => (file, path)
}),
stage := (stage dependsOn dockerGenerateConfig).value,
stagingDirectory := (target in Docker).value / "stage",
dockerLayerMappings := {
val dockerGroups = dockerLayerGrouping.value
val dockerFinalFiles = (mappings in Docker).value
for {
(file, path) <- dockerFinalFiles
layerIdx = dockerGroups(path)
dockerLayerPath = s"/$layerIdx$path"
} yield (layerIdx, file, dockerLayerPath)

},
target := target.value / "docker",
// pick a user name that's unlikely to exist in base images
daemonUser := "demiourgos728",
Expand Down Expand Up @@ -327,22 +356,30 @@ object DockerPlugin extends AutoPlugin {
Cmd("COPY", s"$files /$files")
}

private final def makeCopy(src: String, dest: String): CmdLike =
/**
* This is the file path of the file in the Docker image, and does not depend on the OS where the image
* is being built. This means that it needs to be the Unix file separator even when the image is built
* on e.g. Windows systems.
*/
Cmd("COPY", s"$src $dest")

/**
* @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,19 +524,13 @@ 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 final def mappingsInDocker(universalMappings: Seq[(File, String)],
dockerBaseDirectory: String): Seq[(File, String)] =
for {
(f, path) <- universalMappings
pathInDocker = s"$dockerBaseDirectory/$path"
mappingInDocker = (f, pathInDocker)
} yield mappingInDocker

private[packager] def publishLocalLogger(log: Logger) =
new sys.process.ProcessLogger {
Expand Down Expand Up @@ -716,5 +747,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.")
}