Explorar o código

Merge pull request #22103 from coolljt0725/fix_22093

Fix docker create with duplicate volume failed to remove
Vincent Demeester %!s(int64=9) %!d(string=hai) anos
pai
achega
edcc9577bf
Modificáronse 2 ficheiros con 33 adicións e 1 borrados
  1. 12 1
      daemon/volumes.go
  2. 21 0
      integration-cli/docker_cli_run_test.go

+ 12 - 1
daemon/volumes.go

@@ -65,9 +65,20 @@ func (m mounts) parts(i int) int {
 // 2. Select the volumes mounted from another containers. Overrides previously configured mount point destination.
 // 3. Select the bind mounts set by the client. Overrides previously configured mount point destinations.
 // 4. Cleanup old volumes that are about to be reassigned.
-func (daemon *Daemon) registerMountPoints(container *container.Container, hostConfig *containertypes.HostConfig) error {
+func (daemon *Daemon) registerMountPoints(container *container.Container, hostConfig *containertypes.HostConfig) (retErr error) {
 	binds := map[string]bool{}
 	mountPoints := map[string]*volume.MountPoint{}
+	defer func() {
+		// clean up the container mountpoints once return with error
+		if retErr != nil {
+			for _, m := range mountPoints {
+				if m.Volume == nil {
+					continue
+				}
+				daemon.volumes.Dereference(m.Volume, container.ID)
+			}
+		}
+	}()
 
 	// 1. Read already configured mount points.
 	for name, point := range container.MountPoints {

+ 21 - 0
integration-cli/docker_cli_run_test.go

@@ -546,6 +546,27 @@ func (s *DockerSuite) TestRunNoDupVolumes(c *check.C) {
 			c.Fatalf("Expected 'duplicate mount point' error, got %v", out)
 		}
 	}
+
+	// Test for https://github.com/docker/docker/issues/22093
+	volumename1 := "test1"
+	volumename2 := "test2"
+	volume1 := volumename1 + someplace
+	volume2 := volumename2 + someplace
+	if out, _, err := dockerCmdWithError("run", "-v", volume1, "-v", volume2, "busybox", "true"); err == nil {
+		c.Fatal("Expected error about duplicate mount definitions")
+	} else {
+		if !strings.Contains(out, "Duplicate mount point") {
+			c.Fatalf("Expected 'duplicate mount point' error, got %v", out)
+		}
+	}
+	// create failed should have create volume volumename1 or volumename2
+	// we should remove volumename2 or volumename2 successfully
+	out, _ := dockerCmd(c, "volume", "ls")
+	if strings.Contains(out, volumename1) {
+		dockerCmd(c, "volume", "rm", volumename1)
+	} else {
+		dockerCmd(c, "volume", "rm", volumename2)
+	}
 }
 
 // Test for #1351