Skip to content

Commit

Permalink
Merge pull request moby#40442 from jecepeda/docker-ps-filter-publish-fix
Browse files Browse the repository at this point in the history
Fix docker ps --filter publish
  • Loading branch information
thaJeztah authored May 15, 2020
2 parents c85b2e5 + f48b7d6 commit 8593b3d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
26 changes: 11 additions & 15 deletions daemon/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,23 +551,19 @@ func includeContainerInList(container *container.Snapshot, ctx *listContext) ite
}
}

if len(ctx.publish) > 0 {
shouldSkip := true
for port := range ctx.publish {
if _, ok := container.PortBindings[port]; ok {
if len(ctx.expose) > 0 || len(ctx.publish) > 0 {
var (
shouldSkip bool = true
publishedPort nat.Port
exposedPort nat.Port
)
for _, port := range container.Ports {
publishedPort = nat.Port(fmt.Sprintf("%d/%s", port.PublicPort, port.Type))
exposedPort = nat.Port(fmt.Sprintf("%d/%s", port.PrivatePort, port.Type))
if ok := ctx.publish[publishedPort]; ok {
shouldSkip = false
break
}
}
if shouldSkip {
return excludeContainer
}
}

if len(ctx.expose) > 0 {
shouldSkip := true
for port := range ctx.expose {
if _, ok := container.ExposedPorts[port]; ok {
} else if ok := ctx.expose[exposedPort]; ok {
shouldSkip = false
break
}
Expand Down
17 changes: 16 additions & 1 deletion integration-cli/docker_cli_ps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,29 +816,44 @@ func (s *DockerSuite) TestPsListContainersFilterPorts(c *testing.T) {
out, _ = dockerCmd(c, "run", "-d", "--expose=8080", "busybox", "top")
id2 := strings.TrimSpace(out)

out, _ = dockerCmd(c, "run", "-d", "-p", "1090:90", "busybox", "top")
id3 := strings.TrimSpace(out)

out, _ = dockerCmd(c, "ps", "--no-trunc", "-q")
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id1))
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id2))
assert.Assert(c, strings.Contains(strings.TrimSpace(out), id3))

out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter", "publish=80-8080/udp")
assert.Assert(c, strings.TrimSpace(out) != id1)
assert.Assert(c, strings.TrimSpace(out) != id2)
assert.Assert(c, strings.TrimSpace(out) != id3)

out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter", "expose=8081")
assert.Assert(c, strings.TrimSpace(out) != id1)
assert.Assert(c, strings.TrimSpace(out) != id2)
assert.Assert(c, strings.TrimSpace(out) != id3)

out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter", "publish=80-81")
assert.Equal(c, strings.TrimSpace(out), id1)
assert.Assert(c, strings.TrimSpace(out) != id1)
assert.Assert(c, strings.TrimSpace(out) != id2)
assert.Assert(c, strings.TrimSpace(out) != id3)

out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter", "expose=80/tcp")
assert.Equal(c, strings.TrimSpace(out), id1)
assert.Assert(c, strings.TrimSpace(out) != id2)
assert.Assert(c, strings.TrimSpace(out) != id3)

out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter", "publish=1090")
assert.Assert(c, strings.TrimSpace(out) != id1)
assert.Assert(c, strings.TrimSpace(out) != id2)
assert.Equal(c, strings.TrimSpace(out), id3)

out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter", "expose=8080/tcp")
out = RemoveOutputForExistingElements(out, existingContainers)
assert.Assert(c, strings.TrimSpace(out) != id1)
assert.Equal(c, strings.TrimSpace(out), id2)
assert.Assert(c, strings.TrimSpace(out) != id3)
}

func (s *DockerSuite) TestPsNotShowLinknamesOfDeletedContainer(c *testing.T) {
Expand Down

0 comments on commit 8593b3d

Please sign in to comment.