diff --git a/daemon/list.go b/daemon/list.go index 8de7cf40d7..703ecf0558 100644 --- a/daemon/list.go +++ b/daemon/list.go @@ -381,7 +381,7 @@ func (daemon *Daemon) Volumes(filter string) ([]*types.Volume, error) { volumes := daemon.volumes.List() for _, v := range volumes { - if filterUsed && daemon.volumes.Count(v) == 0 { + if filterUsed && daemon.volumes.Count(v) > 0 { continue } volumesOut = append(volumesOut, volumeToAPIType(v)) diff --git a/integration-cli/docker_cli_volume_test.go b/integration-cli/docker_cli_volume_test.go index e81dc8ffe3..25988f74b2 100644 --- a/integration-cli/docker_cli_volume_test.go +++ b/integration-cli/docker_cli_volume_test.go @@ -4,6 +4,7 @@ import ( "os/exec" "strings" + "github.com/docker/docker/integration-cli/checker" "github.com/go-check/check" ) @@ -54,6 +55,40 @@ func (s *DockerSuite) TestVolumeCliLs(c *check.C) { c.Assert(strings.Contains(out, "test\n"), check.Equals, true) } +func (s *DockerSuite) TestVolumeCliLsFilterDangling(c *check.C) { + testRequires(c, DaemonIsLinux) + + dockerCmd(c, "volume", "create", "--name", "testnotinuse1") + dockerCmd(c, "volume", "create", "--name", "testisinuse1") + dockerCmd(c, "volume", "create", "--name", "testisinuse2") + + // Make sure both "created" (but not started), and started + // containers are included in reference counting + dockerCmd(c, "run", "--name", "volume-test1", "-v", "testisinuse1:/foo", "busybox", "true") + dockerCmd(c, "create", "--name", "volume-test2", "-v", "testisinuse2:/foo", "busybox", "true") + + out, _ := dockerCmd(c, "volume", "ls") + + // No filter, all volumes should show + c.Assert(out, checker.Contains, "testnotinuse1\n", check.Commentf("expected volume 'testnotinuse1' in output")) + c.Assert(out, checker.Contains, "testisinuse1\n", check.Commentf("expected volume 'testisinuse1' in output")) + c.Assert(out, checker.Contains, "testisinuse2\n", check.Commentf("expected volume 'testisinuse2' in output")) + + out, _ = dockerCmd(c, "volume", "ls", "--filter", "dangling=false") + + // Same as above, but expicitly disabling dangling + c.Assert(out, checker.Contains, "testnotinuse1\n", check.Commentf("expected volume 'testnotinuse1' in output")) + c.Assert(out, checker.Contains, "testisinuse1\n", check.Commentf("expected volume 'testisinuse1' in output")) + c.Assert(out, checker.Contains, "testisinuse2\n", check.Commentf("expected volume 'testisinuse2' in output")) + + out, _ = dockerCmd(c, "volume", "ls", "--filter", "dangling=true") + + // Filter "dangling" volumes; ony "dangling" (unused) volumes should be in the output + c.Assert(out, checker.Contains, "testnotinuse1\n", check.Commentf("expected volume 'testnotinuse1' in output")) + c.Assert(out, check.Not(checker.Contains), "testisinuse1\n", check.Commentf("volume 'testisinuse1' in output, but not expected")) + c.Assert(out, check.Not(checker.Contains), "testisinuse2\n", check.Commentf("volume 'testisinuse2' in output, but not expected")) +} + func (s *DockerSuite) TestVolumeCliRm(c *check.C) { testRequires(c, DaemonIsLinux) out, _ := dockerCmd(c, "volume", "create")