Skip to content

Commit edcc957

Browse files
committed
Merge pull request moby#22103 from coolljt0725/fix_22093
Fix docker create with duplicate volume failed to remove
2 parents f3a7abe + 5e5e1d7 commit edcc957

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

daemon/volumes.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,20 @@ func (m mounts) parts(i int) int {
6565
// 2. Select the volumes mounted from another containers. Overrides previously configured mount point destination.
6666
// 3. Select the bind mounts set by the client. Overrides previously configured mount point destinations.
6767
// 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) {
6969
binds := map[string]bool{}
7070
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+
}()
7182

7283
// 1. Read already configured mount points.
7384
for name, point := range container.MountPoints {

integration-cli/docker_cli_run_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,27 @@ func (s *DockerSuite) TestRunNoDupVolumes(c *check.C) {
546546
c.Fatalf("Expected 'duplicate mount point' error, got %v", out)
547547
}
548548
}
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+
}
549570
}
550571

551572
// Test for #1351

0 commit comments

Comments
 (0)