Skip to content

Commit

Permalink
builder filter volumes on run
Browse files Browse the repository at this point in the history
Fixes moby#3639

This makes the builder, on `RUN` commands disable volumes, this way
anything written to dirs that have a volume will persist in the image,
as is the expected behavior in builds.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
  • Loading branch information
cpuguy83 committed Mar 6, 2015
1 parent e0823b0 commit 84a81ec
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
9 changes: 9 additions & 0 deletions builder/dispatchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,19 @@ func run(b *Builder, args []string, attributes map[string]bool, original string)
c.Mount()
defer c.Unmount()

// Unset the volumes
volumeConfig := c.Config.Volumes
c.Config.Volumes = nil
volumes := c.Volumes
c.Volumes = nil

err = b.run(c)
if err != nil {
return err
}
c.Volumes = volumes
c.Config.Volumes = volumeConfig

if err := b.commit(c.ID, cmd, "run"); err != nil {
return err
}
Expand Down
10 changes: 10 additions & 0 deletions builder/internals.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ func (b *Builder) commit(id string, autoCmd []string, comment string) error {
return err
}
defer container.Unmount()

// Make sure all resources are setup, but we can ignore errors
// Supress output from this
errStream := b.ErrStream
outStream := b.OutStream
b.ErrStream = nil
b.OutStream = nil
b.run(container)
b.ErrStream = errStream
b.OutStream = outStream
}
container, err := b.Daemon.Get(id)
if err != nil {
Expand Down
49 changes: 48 additions & 1 deletion integration-cli/docker_cli_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4345,7 +4345,7 @@ func TestBuildExoticShellInterpolation(t *testing.T) {

_, err := buildImage(name, `
FROM busybox
ENV SOME_VAR a.b.c
RUN [ "$SOME_VAR" = 'a.b.c' ]
Expand Down Expand Up @@ -5115,3 +5115,50 @@ func TestBuildNotVerbose(t *testing.T) {

logDone("build - not verbose")
}

func TestBuildVolumesFiltered(t *testing.T) {
defer func() {
deleteAllContainers()
deleteImages("test")
}()

ctx, err := fakeContext(`
FROM busybox
VOLUME /foo
RUN [ -d /foo ]
RUN touch /foo/bar
RUN [ -f /foo/bar ]
ADD baz /foo/baz
RUN [ -f /foo/baz ]
`,
map[string]string{"baz": "qux"})
defer ctx.Close()

if err != nil {
t.Fatal(err)
}

if _, err = buildImageFromContext("test", ctx, false); err != nil {
t.Fatalf("expected build to succeed:\n%v", err)
}

out, _, err := dockerCmd(t, "run", "--name=voltest", "test", "/bin/sh", "-c", "cat /foo/baz")
if err != nil {
t.Fatal(err, out)
}

if out != "qux" {
t.Fatal("unexpected output from container:\n%s", out)
}

volPath, err := inspectFieldMap("voltest", "Volumes", "/foo")
if err != nil {
t.Fatal(err)
}

if volPath == "<no value>" || !strings.HasPrefix(volPath, "/") {
t.Fatalf("expected volume at /foo, got: %s", volPath)
}

logDone("build - volumes are filtered on run")
}

0 comments on commit 84a81ec

Please sign in to comment.