Skip to content

Commit

Permalink
docker: make None the last layer group (#1370)
Browse files Browse the repository at this point in the history
Currently, if a user adds additional artifact to the Docker build
without modifying dockerGroupLayers, the artifact will have "None"
grouping and be put as the first layer in the output.

These additional artifacts are often configuration or supporting
assets. Making this the zeroth layer by default causes dependency
layer to not be re-used if any of these artifacts are changed.

Since not all users will care to modify dockerGroupLayers, the
default should be to make it such that adding additional artifact
does not break re-use of the dependency layer.

Co-authored-by: Nepomuk Seiler <muuki88@users.noreply.github.com>
  • Loading branch information
pawitp and muuki88 committed Oct 5, 2020
1 parent 1ee8481 commit 2e77328
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ object DockerPlugin extends AutoPlugin {
val generalCommands = makeFromAs(base, "mainstage") +: makeMaintainer((maintainer in Docker).value).toSeq
val stage0name = "stage0"
val layerMappings = (dockerLayerMappings in Docker).value
val layerIdsAscending = layerMappings.map(_.layerId).distinct.sorted
val layerIdsAscending = layerMappings.map(_.layerId).distinct.sortWith { (a, b) =>
// Make the None (unspecified) layer the last layer
a.getOrElse(Int.MaxValue) < b.getOrElse(Int.MaxValue)
}
val stage0: Seq[CmdLike] = strategy match {
case DockerPermissionStrategy.MultiStage =>
Seq(
Expand Down
26 changes: 25 additions & 1 deletion src/sbt-test/docker/test-layer-groups/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,28 @@ dockerPackageMappings in Docker ++= Seq(
(baseDirectory.value / "docker" / "log4j.properties") -> "/opt/docker/other/log4j.properties"
)

libraryDependencies += "org.slf4j" % "slf4j-api" % "1.7.30"
libraryDependencies += "org.slf4j" % "slf4j-api" % "1.7.30"

TaskKey[Unit]("checkDockerfile") := {
val dockerfile = IO.read((stagingDirectory in Docker).value / "Dockerfile")
val copyLines = dockerfile.linesIterator.toList.filter(_.startsWith("COPY --from=stage0"))
assertEquals(copyLines,
"""COPY --from=stage0 --chown=demiourgos728:root /1/opt/docker /opt/docker
|COPY --from=stage0 --chown=demiourgos728:root /2/opt/docker /opt/docker
|COPY --from=stage0 --chown=demiourgos728:root /54/opt/docker /opt/docker
|COPY --from=stage0 --chown=demiourgos728:root /opt/docker /opt/docker""".stripMargin.linesIterator.toList)
}

TaskKey[Unit]("checkDockerfileWithNoLayers") := {
val dockerfile = IO.read((stagingDirectory in Docker).value / "Dockerfile")
val copyLines = dockerfile.linesIterator.toList.filter(_.startsWith("COPY --from=stage0"))
assertEquals(copyLines,
"""COPY --from=stage0 --chown=demiourgos728:root /opt/docker /opt/docker""".stripMargin.linesIterator.toList)
}

def assertEquals(left: List[String], right: List[String]) =
assert(left == right,
"\n" + ((left zip right) flatMap { case (a: String, b: String) =>
if (a == b) Nil
else List("- " + a, "+ " + b)
}).mkString("\n"))
2 changes: 2 additions & 0 deletions src/sbt-test/docker/test-layer-groups/test
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ $ exists target/docker/stage/54/opt/docker/spark

$ exec bash -c 'docker run --rm --entrypoint=ls docker-groups:0.1.0 |tr "\n" "," | grep -q "bin,lib,other,spark"'
$ exec bash -c 'docker rmi docker-groups:0.1.0'
> checkDockerfile

$ copy-file changes/nolayers.sbt layers.sbt
> reload
Expand All @@ -25,3 +26,4 @@ $ exists target/docker/stage/opt/docker/spark
$ exists target/docker/stage/opt/docker/lib/org.slf4j.slf4j-api-1.7.30.jar
$ exists target/docker/stage/opt/docker/lib/com.example.docker-groups-0.1.0.jar
$ exec bash -c 'docker run --rm --entrypoint=ls docker-groups:0.1.0 |tr "\n" "," | grep -q "bin,lib,other,spark"'
> checkDockerfileWithNoLayers

0 comments on commit 2e77328

Please sign in to comment.