Переглянути джерело

Merge pull request #4752 from crosbymichael/fix-volumes-from-files

Allow volumes from to be individual files
unclejack 11 роки тому
батько
коміт
b936f4f5e1
2 змінених файлів з 47 додано та 19 видалено
  1. 18 0
      integration-cli/docker_cli_run_test.go
  2. 29 19
      runtime/volumes.go

+ 18 - 0
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")
+}

+ 29 - 19
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
+}