1
0
mirror of https://github.com/moby/moby.git synced 2025-07-29 07:21:35 +03:00

Adds ability to squash image after build

Allow built images to be squash to scratch.
Squashing does not destroy any images or layers, and preserves the
build cache.

Introduce a new CLI argument --squash to docker build
Introduce a new param to the build API endpoint `squash`

Once the build is complete, docker creates a new image loading the diffs
from each layer into a single new layer and references all the parent's
layers.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
Brian Goff
2016-04-21 12:08:37 -04:00
parent 2fc760496b
commit 362369b4bb
21 changed files with 330 additions and 23 deletions

View File

@ -7195,3 +7195,44 @@ RUN ["cat", "/foo/file"]
c.Fatal(err)
}
}
func (s *DockerSuite) TestBuildSquashParent(c *check.C) {
testRequires(c, ExperimentalDaemon)
dockerFile := `
FROM busybox
RUN echo hello > /hello
RUN echo world >> /hello
RUN echo hello > /remove_me
ENV HELLO world
RUN rm /remove_me
`
// build and get the ID that we can use later for history comparison
origID, err := buildImage("test", dockerFile, false)
c.Assert(err, checker.IsNil)
// build with squash
id, err := buildImage("test", dockerFile, true, "--squash")
c.Assert(err, checker.IsNil)
out, _ := dockerCmd(c, "run", "--rm", id, "/bin/sh", "-c", "cat /hello")
c.Assert(strings.TrimSpace(out), checker.Equals, "hello\nworld")
dockerCmd(c, "run", "--rm", id, "/bin/sh", "-c", "[ ! -f /remove_me ]")
dockerCmd(c, "run", "--rm", id, "/bin/sh", "-c", `[ "$(echo $HELLO)" == "world" ]`)
// make sure the ID produced is the ID of the tag we specified
inspectID, err := inspectImage("test", ".ID")
c.Assert(err, checker.IsNil)
c.Assert(inspectID, checker.Equals, id)
origHistory, _ := dockerCmd(c, "history", origID)
testHistory, _ := dockerCmd(c, "history", "test")
splitOrigHistory := strings.Split(strings.TrimSpace(origHistory), "\n")
splitTestHistory := strings.Split(strings.TrimSpace(testHistory), "\n")
c.Assert(len(splitTestHistory), checker.Equals, len(splitOrigHistory)+1)
out, err = inspectImage(id, "len .RootFS.Layers")
c.Assert(err, checker.IsNil)
c.Assert(strings.TrimSpace(out), checker.Equals, "3")
}