Browse code

Fix docker create with duplicate volume failed to remove

Signed-off-by: Lei Jitang <leijitang@huawei.com>

Lei Jitang authored on 2016/05/07 11:48:02
Showing 2 changed files
... ...
@@ -65,9 +65,20 @@ func (m mounts) parts(i int) int {
65 65
 // 2. Select the volumes mounted from another containers. Overrides previously configured mount point destination.
66 66
 // 3. Select the bind mounts set by the client. Overrides previously configured mount point destinations.
67 67
 // 4. Cleanup old volumes that are about to be reassigned.
68
-func (daemon *Daemon) registerMountPoints(container *container.Container, hostConfig *containertypes.HostConfig) error {
68
+func (daemon *Daemon) registerMountPoints(container *container.Container, hostConfig *containertypes.HostConfig) (retErr error) {
69 69
 	binds := map[string]bool{}
70 70
 	mountPoints := map[string]*volume.MountPoint{}
71
+	defer func() {
72
+		// clean up the container mountpoints once return with error
73
+		if retErr != nil {
74
+			for _, m := range mountPoints {
75
+				if m.Volume == nil {
76
+					continue
77
+				}
78
+				daemon.volumes.Dereference(m.Volume, container.ID)
79
+			}
80
+		}
81
+	}()
71 82
 
72 83
 	// 1. Read already configured mount points.
73 84
 	for name, point := range container.MountPoints {
... ...
@@ -531,6 +531,27 @@ func (s *DockerSuite) TestRunNoDupVolumes(c *check.C) {
531 531
 			c.Fatalf("Expected 'duplicate mount point' error, got %v", out)
532 532
 		}
533 533
 	}
534
+
535
+	// Test for https://github.com/docker/docker/issues/22093
536
+	volumename1 := "test1"
537
+	volumename2 := "test2"
538
+	volume1 := volumename1 + someplace
539
+	volume2 := volumename2 + someplace
540
+	if out, _, err := dockerCmdWithError("run", "-v", volume1, "-v", volume2, "busybox", "true"); err == nil {
541
+		c.Fatal("Expected error about duplicate mount definitions")
542
+	} else {
543
+		if !strings.Contains(out, "Duplicate mount point") {
544
+			c.Fatalf("Expected 'duplicate mount point' error, got %v", out)
545
+		}
546
+	}
547
+	// create failed should have create volume volumename1 or volumename2
548
+	// we should remove volumename2 or volumename2 successfully
549
+	out, _ := dockerCmd(c, "volume", "ls")
550
+	if strings.Contains(out, volumename1) {
551
+		dockerCmd(c, "volume", "rm", volumename1)
552
+	} else {
553
+		dockerCmd(c, "volume", "rm", volumename2)
554
+	}
534 555
 }
535 556
 
536 557
 // Test for #1351