Browse code

Merge pull request #22103 from coolljt0725/fix_22093

Fix docker create with duplicate volume failed to remove

Vincent Demeester authored on 2016/05/30 22:57:13
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 {
... ...
@@ -546,6 +546,27 @@ func (s *DockerSuite) TestRunNoDupVolumes(c *check.C) {
546 546
 			c.Fatalf("Expected 'duplicate mount point' error, got %v", out)
547 547
 		}
548 548
 	}
549
+
550
+	// Test for https://github.com/docker/docker/issues/22093
551
+	volumename1 := "test1"
552
+	volumename2 := "test2"
553
+	volume1 := volumename1 + someplace
554
+	volume2 := volumename2 + someplace
555
+	if out, _, err := dockerCmdWithError("run", "-v", volume1, "-v", volume2, "busybox", "true"); err == nil {
556
+		c.Fatal("Expected error about duplicate mount definitions")
557
+	} else {
558
+		if !strings.Contains(out, "Duplicate mount point") {
559
+			c.Fatalf("Expected 'duplicate mount point' error, got %v", out)
560
+		}
561
+	}
562
+	// create failed should have create volume volumename1 or volumename2
563
+	// we should remove volumename2 or volumename2 successfully
564
+	out, _ := dockerCmd(c, "volume", "ls")
565
+	if strings.Contains(out, volumename1) {
566
+		dockerCmd(c, "volume", "rm", volumename1)
567
+	} else {
568
+		dockerCmd(c, "volume", "rm", volumename2)
569
+	}
549 570
 }
550 571
 
551 572
 // Test for #1351