Adding network filter to docker ps command

Signed-off-by: Sainath Grandhi <sainath.grandhi@intel.com>
This commit is contained in:
Sainath Grandhi 2016-05-27 12:20:31 -07:00 committed by Sebastiaan van Stijn
parent a01ae049f9
commit 912af1ae8f
6 changed files with 66 additions and 1 deletions

View File

@ -33,6 +33,7 @@ var acceptedPsFilterTags = map[string]bool{
"status": true, "status": true,
"since": true, "since": true,
"volume": true, "volume": true,
"network": true,
} }
// iterationAction represents possible outcomes happening during the container iteration. // iterationAction represents possible outcomes happening during the container iteration.
@ -374,6 +375,19 @@ func includeContainerInList(container *container.Container, ctx *listContext) it
} }
} }
networkExist := fmt.Errorf("container part of network")
if ctx.filters.Include("network") {
err := ctx.filters.WalkValues("network", func(value string) error {
if network := container.NetworkSettings.Networks[value]; network != nil {
return networkExist
}
return nil
})
if err != networkExist {
return excludeContainer
}
}
return includeContainer return includeContainer
} }

View File

@ -117,6 +117,7 @@ This section lists each version from latest to oldest. Each listing includes a
* `POST /containers/create` now takes `StorageOpt` field. * `POST /containers/create` now takes `StorageOpt` field.
* `GET /info` now returns `SecurityOptions` field, showing if `apparmor`, `seccomp`, or `selinux` is supported. * `GET /info` now returns `SecurityOptions` field, showing if `apparmor`, `seccomp`, or `selinux` is supported.
* `GET /networks` now supports filtering by `label` and `driver`. * `GET /networks` now supports filtering by `label` and `driver`.
* `GET /containers/json` now supports filtering containers by `network` name.
* `POST /containers/create` now takes `MaximumIOps` and `MaximumIOBps` fields. Windows daemon only. * `POST /containers/create` now takes `MaximumIOps` and `MaximumIOBps` fields. Windows daemon only.
* `POST /containers/create` now returns an HTTP 400 "bad parameter" message * `POST /containers/create` now returns an HTTP 400 "bad parameter" message
if no command is specified (instead of an HTTP 500 "server error") if no command is specified (instead of an HTTP 500 "server error")

View File

@ -223,6 +223,7 @@ Query Parameters:
- `before`=(`<container id>` or `<container name>`) - `before`=(`<container id>` or `<container name>`)
- `since`=(`<container id>` or `<container name>`) - `since`=(`<container id>` or `<container name>`)
- `volume`=(`<volume name>` or `<mount point destination>`) - `volume`=(`<volume name>` or `<mount point destination>`)
- `network`=(`<network name>`)
Status Codes: Status Codes:

View File

@ -62,7 +62,7 @@ The currently supported filters are:
* since (container's id or name) - filters containers created since given id or name * since (container's id or name) - filters containers created since given id or name
* isolation (default|process|hyperv) (Windows daemon only) * isolation (default|process|hyperv) (Windows daemon only)
* volume (volume name or mount point) - filters containers that mount volumes. * volume (volume name or mount point) - filters containers that mount volumes.
* network (network name) - filters containers connected to the provided network name
#### Label #### Label
@ -207,6 +207,17 @@ The `volume` filter shows only containers that mount a specific volume or have a
CONTAINER ID MOUNTS CONTAINER ID MOUNTS
9c3527ed70ce remote-volume 9c3527ed70ce remote-volume
#### Network
The `network` filter shows only containers that has endpoints on the provided network name.
$docker run -d --net=net1 --name=test1 ubuntu top
$docker run -d --net=net2 --name=test2 ubuntu top
$docker ps --filter network=net1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9d4893ed80fe ubuntu "top" 10 minutes ago Up 10 minutes test1
## Formatting ## Formatting

View File

@ -804,3 +804,40 @@ func (s *DockerSuite) TestPsFormatSize(c *check.C) {
lines = strings.Split(out, "\n") lines = strings.Split(out, "\n")
c.Assert(lines[8], checker.HasPrefix, "size:", check.Commentf("Size should be appended on a newline")) c.Assert(lines[8], checker.HasPrefix, "size:", check.Commentf("Size should be appended on a newline"))
} }
func (s *DockerSuite) TestPsListContainersFilterNetwork(c *check.C) {
// create a container
out, _ := runSleepingContainer(c, "--net=bridge", "--name=onbridgenetwork")
out, _ = runSleepingContainer(c, "--net=none", "--name=onnonenetwork")
// Filter docker ps on network bridge
out, _ = dockerCmd(c, "ps", "--filter", "network=bridge")
containerOut := strings.TrimSpace(string(out))
lines := strings.Split(containerOut, "\n")
// skip header
lines = lines[1:]
// ps output should have only one container
c.Assert(lines, checker.HasLen, 1)
// Making sure onbridgenetwork is on the output
c.Assert(lines[0], checker.Contains, "onbridgenetwork", check.Commentf("Missing the container on network\n"))
// Filter docker ps on networks bridge and none
out, _ = dockerCmd(c, "ps", "--filter", "network=bridge", "--filter", "network=none")
containerOut = strings.TrimSpace(string(out))
lines = strings.Split(containerOut, "\n")
// skip header
lines = lines[1:]
//ps output should have both the containers
c.Assert(lines, checker.HasLen, 2)
// Making sure onbridgenetwork and onnonenetwork is on the output
c.Assert(lines[0], checker.Contains, "onnonenetwork", check.Commentf("Missing the container on none network\n"))
c.Assert(lines[1], checker.Contains, "onbridgenetwork", check.Commentf("Missing the container on bridge network\n"))
}

View File

@ -36,6 +36,7 @@ the running containers.
- since=(<container-name>|<container-id>) - since=(<container-name>|<container-id>)
- ancestor=(<image-name>[:tag]|<image-id>|<image@digest>) - containers created from an image or a descendant. - ancestor=(<image-name>[:tag]|<image-id>|<image@digest>) - containers created from an image or a descendant.
- volume=(<volume-name>|<mount-point-destination>) - volume=(<volume-name>|<mount-point-destination>)
- network=(<network-name>) - containers connected to the provided network name
**--format**="*TEMPLATE*" **--format**="*TEMPLATE*"
Pretty-print containers using a Go template. Pretty-print containers using a Go template.