diff --git a/daemon/list.go b/daemon/list.go index 6d86cd3b48..46d7eb18b8 100644 --- a/daemon/list.go +++ b/daemon/list.go @@ -21,6 +21,7 @@ var acceptedVolumeFilterTags = map[string]bool{ "dangling": true, "name": true, "driver": true, + "label": true, } var acceptedPsFilterTags = map[string]bool{ @@ -587,6 +588,15 @@ func (daemon *Daemon) filterVolumes(vols []volume.Volume, filter filters.Args) ( continue } } + if filter.Include("label") { + v, ok := vol.(volume.LabeledVolume) + if !ok { + continue + } + if !filter.MatchKVList("label", v.Labels()) { + continue + } + } retVols = append(retVols, vol) } danglingOnly := false diff --git a/integration-cli/docker_cli_volume_test.go b/integration-cli/docker_cli_volume_test.go index 9e62fc731b..bdbabb0644 100644 --- a/integration-cli/docker_cli_volume_test.go +++ b/integration-cli/docker_cli_volume_test.go @@ -344,3 +344,33 @@ func (s *DockerSuite) TestVolumeCliCreateLabelMultiple(c *check.C) { c.Assert(strings.TrimSpace(out), check.Equals, v) } } + +func (s *DockerSuite) TestVolumeCliLsFilterLabels(c *check.C) { + testVol1 := "testvolcreatelabel-1" + out, _, err := dockerCmdWithError("volume", "create", "--label", "foo=bar1", "--name", testVol1) + c.Assert(err, check.IsNil) + + testVol2 := "testvolcreatelabel-2" + out, _, err = dockerCmdWithError("volume", "create", "--label", "foo=bar2", "--name", testVol2) + c.Assert(err, check.IsNil) + + out, _ = dockerCmd(c, "volume", "ls", "--filter", "label=foo") + + // filter with label=key + c.Assert(out, checker.Contains, "testvolcreatelabel-1\n", check.Commentf("expected volume 'testvolcreatelabel-1' in output")) + c.Assert(out, checker.Contains, "testvolcreatelabel-2\n", check.Commentf("expected volume 'testvolcreatelabel-2' in output")) + + out, _ = dockerCmd(c, "volume", "ls", "--filter", "label=foo=bar1") + + // filter with label=key=value + c.Assert(out, checker.Contains, "testvolcreatelabel-1\n", check.Commentf("expected volume 'testvolcreatelabel-1' in output")) + c.Assert(out, check.Not(checker.Contains), "testvolcreatelabel-2\n", check.Commentf("expected volume 'testvolcreatelabel-2 in output")) + + out, _ = dockerCmd(c, "volume", "ls", "--filter", "label=non-exist") + outArr := strings.Split(strings.TrimSpace(out), "\n") + c.Assert(len(outArr), check.Equals, 1, check.Commentf("\n%s", out)) + + out, _ = dockerCmd(c, "volume", "ls", "--filter", "label=foo=non-exist") + outArr = strings.Split(strings.TrimSpace(out), "\n") + c.Assert(len(outArr), check.Equals, 1, check.Commentf("\n%s", out)) +}