From 5e5e1d7adae4af11dfa0b39ecc621b0c1fe3f85a Mon Sep 17 00:00:00 2001 From: Lei Jitang Date: Fri, 6 May 2016 22:48:02 -0400 Subject: [PATCH] Fix docker create with duplicate volume failed to remove Signed-off-by: Lei Jitang --- daemon/volumes.go | 13 ++++++++++++- integration-cli/docker_cli_run_test.go | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/daemon/volumes.go b/daemon/volumes.go index b38fd50631..d3fb2c9645 100644 --- a/daemon/volumes.go +++ b/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 { diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index 0cc9c8210b..54fdc42a7a 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -531,6 +531,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