From c73e3bf4dc0382cd188db3e51c52eaf8bf605795 Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Wed, 14 Jan 2015 22:30:28 -0500 Subject: [PATCH] Error out if file in container at volume path Fixes #4393 Signed-off-by: Brian Goff --- daemon/volumes.go | 6 ++++++ integration-cli/docker_cli_build_test.go | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/daemon/volumes.go b/daemon/volumes.go index c6f1b9930a..c72399da6b 100644 --- a/daemon/volumes.go +++ b/daemon/volumes.go @@ -184,6 +184,12 @@ func (container *Container) parseVolumeMountConfig() (map[string]*Mount, error) continue } + if stat, err := os.Stat(filepath.Join(container.basefs, path)); err == nil { + if !stat.IsDir() { + return nil, fmt.Errorf("file exists at %s, can't create volume there") + } + } + vol, err := container.daemon.volumes.FindOrCreateVolume("", true) if err != nil { return nil, err diff --git a/integration-cli/docker_cli_build_test.go b/integration-cli/docker_cli_build_test.go index cbd0a40c15..783c12c288 100644 --- a/integration-cli/docker_cli_build_test.go +++ b/integration-cli/docker_cli_build_test.go @@ -4784,3 +4784,20 @@ RUN echo " \ logDone("build - test spaces with quotes") } + +// #4393 +func TestBuildVolumeFileExistsinContainer(t *testing.T) { + buildCmd := exec.Command(dockerBinary, "build", "-t", "docker-test-errcreatevolumewithfile", "-") + buildCmd.Stdin = strings.NewReader(` + FROM busybox + RUN touch /foo + VOLUME /foo + `) + + out, _, err := runCommandWithOutput(buildCmd) + if err == nil || !strings.Contains(out, "file exists") { + t.Fatalf("expected build to fail when file exists in container at requested volume path") + } + + logDone("build - errors when volume is specified where a file exists") +}