From a57900e35f2c30026a070fdfdbdb0ce99b35e1ff Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Tue, 18 Mar 2014 15:28:40 -0700 Subject: [PATCH 1/2] Allow volumes from to be individual files Fixes #4741 Right now volumes from expected a dir and not a file so when the drivers tried to do the bind mount, the destination was a dir, not a file so it fails to run. Docker-DCO-1.1-Signed-off-by: Michael Crosby (github: crosbymichael) --- runtime/volumes.go | 48 ++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/runtime/volumes.go b/runtime/volumes.go index 1bbb14a369..5ac82ef089 100644 --- a/runtime/volumes.go +++ b/runtime/volumes.go @@ -88,7 +88,11 @@ func applyVolumesFrom(container *Container) error { if _, exists := container.Volumes[volPath]; exists { continue } - if err := os.MkdirAll(filepath.Join(container.basefs, volPath), 0755); err != nil { + stat, err := os.Stat(filepath.Join(c.basefs, volPath)) + if err != nil { + return err + } + if err := createIfNotExists(filepath.Join(container.basefs, volPath), stat.IsDir()); err != nil { return err } container.Volumes[volPath] = id @@ -208,24 +212,8 @@ func createVolumes(container *Container) error { if err != nil { return err } - - if _, err := os.Stat(rootVolPath); err != nil { - if os.IsNotExist(err) { - if volIsDir { - if err := os.MkdirAll(rootVolPath, 0755); err != nil { - return err - } - } else { - if err := os.MkdirAll(filepath.Dir(rootVolPath), 0755); err != nil { - return err - } - if f, err := os.OpenFile(rootVolPath, os.O_CREATE, 0755); err != nil { - return err - } else { - f.Close() - } - } - } + if err := createIfNotExists(rootVolPath, volIsDir); err != nil { + return err } // Do not copy or change permissions if we are mounting from the host @@ -266,3 +254,25 @@ func createVolumes(container *Container) error { } return nil } + +func createIfNotExists(path string, isDir bool) error { + if _, err := os.Stat(path); err != nil { + if os.IsNotExist(err) { + if isDir { + if err := os.MkdirAll(path, 0755); err != nil { + return err + } + } else { + if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { + return err + } + f, err := os.OpenFile(path, os.O_CREATE, 0755) + if err != nil { + return err + } + defer f.Close() + } + } + } + return nil +} From 28015f8e579e7bbe396f65b3343188ca03b06cbd Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Mon, 31 Mar 2014 17:41:40 +0000 Subject: [PATCH 2/2] Add integration test for volumes-from as file Docker-DCO-1.1-Signed-off-by: Michael Crosby (github: crosbymichael) --- integration-cli/docker_cli_run_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index 12915d72ff..13959adea7 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -253,3 +253,21 @@ func TestDockerRunWithoutNetworking(t *testing.T) { logDone("run - disable networking with --networking=false") logDone("run - disable networking with -n=false") } + +// Regression test for #4741 +func TestDockerRunWithVolumesAsFiles(t *testing.T) { + runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/etc/hosts:/target-file", "busybox", "true") + out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd) + if err != nil && exitCode != 0 { + t.Fatal("1", out, stderr, err) + } + + runCmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-data", "busybox", "cat", "/target-file") + out, stderr, exitCode, err = runCommandWithStdoutStderr(runCmd) + if err != nil && exitCode != 0 { + t.Fatal("2", out, stderr, err) + } + deleteAllContainers() + + logDone("run - regression test for #4741 - volumes from as files") +}