mirror of
https://github.com/moby/moby.git
synced 2025-07-30 18:23:29 +03:00
Add support for COPY from previous rootfs
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
@ -5755,6 +5755,138 @@ func (s *DockerSuite) TestBuildContChar(c *check.C) {
|
||||
c.Assert(result.Combined(), checker.Contains, "Step 2/2 : RUN echo hi \\\\\n")
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestBuildCopyFromPreviousRootFS(c *check.C) {
|
||||
dockerfile := `
|
||||
FROM busybox
|
||||
COPY foo bar
|
||||
FROM busybox
|
||||
%s
|
||||
COPY baz baz
|
||||
RUN echo mno > baz/cc
|
||||
FROM busybox
|
||||
COPY bar /
|
||||
COPY --from=1 baz sub/
|
||||
COPY --from=0 bar baz
|
||||
COPY --from=0 bar bay`
|
||||
ctx := fakeContext(c, fmt.Sprintf(dockerfile, ""), map[string]string{
|
||||
"Dockerfile": dockerfile,
|
||||
"foo": "abc",
|
||||
"bar": "def",
|
||||
"baz/aa": "ghi",
|
||||
"baz/bb": "jkl",
|
||||
})
|
||||
defer ctx.Close()
|
||||
|
||||
result := buildImage("build1", withExternalBuildContext(ctx))
|
||||
result.Assert(c, icmd.Success)
|
||||
|
||||
out, _ := dockerCmd(c, "run", "build1", "cat", "bar")
|
||||
c.Assert(strings.TrimSpace(out), check.Equals, "def")
|
||||
out, _ = dockerCmd(c, "run", "build1", "cat", "sub/aa")
|
||||
c.Assert(strings.TrimSpace(out), check.Equals, "ghi")
|
||||
out, _ = dockerCmd(c, "run", "build1", "cat", "sub/cc")
|
||||
c.Assert(strings.TrimSpace(out), check.Equals, "mno")
|
||||
out, _ = dockerCmd(c, "run", "build1", "cat", "baz")
|
||||
c.Assert(strings.TrimSpace(out), check.Equals, "abc")
|
||||
out, _ = dockerCmd(c, "run", "build1", "cat", "bay")
|
||||
c.Assert(strings.TrimSpace(out), check.Equals, "abc")
|
||||
|
||||
result = buildImage("build2", withExternalBuildContext(ctx))
|
||||
result.Assert(c, icmd.Success)
|
||||
|
||||
// all commands should be cached
|
||||
c.Assert(strings.Count(result.Combined(), "Using cache"), checker.Equals, 7)
|
||||
|
||||
err := ioutil.WriteFile(filepath.Join(ctx.Dir, "Dockerfile"), []byte(fmt.Sprintf(dockerfile, "COPY baz/aa foo")), 0644)
|
||||
c.Assert(err, checker.IsNil)
|
||||
|
||||
// changing file in parent block should not affect last block
|
||||
result = buildImage("build3", withExternalBuildContext(ctx))
|
||||
result.Assert(c, icmd.Success)
|
||||
|
||||
c.Assert(strings.Count(result.Combined(), "Using cache"), checker.Equals, 5)
|
||||
|
||||
c.Assert(getIDByName(c, "build1"), checker.Equals, getIDByName(c, "build2"))
|
||||
|
||||
err = ioutil.WriteFile(filepath.Join(ctx.Dir, "foo"), []byte("pqr"), 0644)
|
||||
c.Assert(err, checker.IsNil)
|
||||
|
||||
// changing file in parent block should affect both first and last block
|
||||
result = buildImage("build4", withExternalBuildContext(ctx))
|
||||
result.Assert(c, icmd.Success)
|
||||
c.Assert(strings.Count(result.Combined(), "Using cache"), checker.Equals, 5)
|
||||
|
||||
out, _ = dockerCmd(c, "run", "build4", "cat", "bay")
|
||||
c.Assert(strings.TrimSpace(out), check.Equals, "pqr")
|
||||
out, _ = dockerCmd(c, "run", "build4", "cat", "baz")
|
||||
c.Assert(strings.TrimSpace(out), check.Equals, "pqr")
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestBuildCopyFromPreviousRootFSErrors(c *check.C) {
|
||||
dockerfile := `
|
||||
FROM busybox
|
||||
COPY --from=foo foo bar`
|
||||
|
||||
ctx := fakeContext(c, dockerfile, map[string]string{
|
||||
"Dockerfile": dockerfile,
|
||||
"foo": "abc",
|
||||
})
|
||||
defer ctx.Close()
|
||||
|
||||
buildImage("build1", withExternalBuildContext(ctx)).Assert(c, icmd.Expected{
|
||||
ExitCode: 1,
|
||||
Err: "from expects an integer value corresponding to the context number",
|
||||
})
|
||||
|
||||
dockerfile = `
|
||||
FROM busybox
|
||||
COPY --from=0 foo bar`
|
||||
|
||||
ctx = fakeContext(c, dockerfile, map[string]string{
|
||||
"Dockerfile": dockerfile,
|
||||
"foo": "abc",
|
||||
})
|
||||
defer ctx.Close()
|
||||
|
||||
buildImage("build1", withExternalBuildContext(ctx)).Assert(c, icmd.Expected{
|
||||
ExitCode: 1,
|
||||
Err: "invalid from flag value 0 refers current build block",
|
||||
})
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestBuildCopyFromPreviousFrom(c *check.C) {
|
||||
dockerfile := `
|
||||
FROM busybox
|
||||
COPY foo bar`
|
||||
ctx := fakeContext(c, dockerfile, map[string]string{
|
||||
"Dockerfile": dockerfile,
|
||||
"foo": "abc",
|
||||
})
|
||||
defer ctx.Close()
|
||||
|
||||
result := buildImage("build1", withExternalBuildContext(ctx))
|
||||
result.Assert(c, icmd.Success)
|
||||
|
||||
dockerfile = `
|
||||
FROM build1:latest
|
||||
FROM busybox
|
||||
COPY --from=0 bar /
|
||||
COPY foo /`
|
||||
ctx = fakeContext(c, dockerfile, map[string]string{
|
||||
"Dockerfile": dockerfile,
|
||||
"foo": "def",
|
||||
})
|
||||
defer ctx.Close()
|
||||
|
||||
result = buildImage("build2", withExternalBuildContext(ctx))
|
||||
result.Assert(c, icmd.Success)
|
||||
|
||||
out, _ := dockerCmd(c, "run", "build2", "cat", "bar")
|
||||
c.Assert(strings.TrimSpace(out), check.Equals, "abc")
|
||||
out, _ = dockerCmd(c, "run", "build2", "cat", "foo")
|
||||
c.Assert(strings.TrimSpace(out), check.Equals, "def")
|
||||
}
|
||||
|
||||
// TestBuildOpaqueDirectory tests that a build succeeds which
|
||||
// creates opaque directories.
|
||||
// See https://github.com/docker/docker/issues/25244
|
||||
|
Reference in New Issue
Block a user