Browse code

Adding network filter to docker ps command

Signed-off-by: Sainath Grandhi <sainath.grandhi@intel.com>

Sainath Grandhi authored on 2016/05/28 04:20:31
Showing 6 changed files
... ...
@@ -33,6 +33,7 @@ var acceptedPsFilterTags = map[string]bool{
33 33
 	"status":    true,
34 34
 	"since":     true,
35 35
 	"volume":    true,
36
+	"network":   true,
36 37
 }
37 38
 
38 39
 // iterationAction represents possible outcomes happening during the container iteration.
... ...
@@ -374,6 +375,19 @@ func includeContainerInList(container *container.Container, ctx *listContext) it
374 374
 		}
375 375
 	}
376 376
 
377
+	networkExist := fmt.Errorf("container part of network")
378
+	if ctx.filters.Include("network") {
379
+		err := ctx.filters.WalkValues("network", func(value string) error {
380
+			if network := container.NetworkSettings.Networks[value]; network != nil {
381
+				return networkExist
382
+			}
383
+			return nil
384
+		})
385
+		if err != networkExist {
386
+			return excludeContainer
387
+		}
388
+	}
389
+
377 390
 	return includeContainer
378 391
 }
379 392
 
... ...
@@ -117,6 +117,7 @@ This section lists each version from latest to oldest.  Each listing includes a
117 117
 * `POST /containers/create` now takes `StorageOpt` field.
118 118
 * `GET /info` now returns `SecurityOptions` field, showing if `apparmor`, `seccomp`, or `selinux` is supported.
119 119
 * `GET /networks` now supports filtering by `label` and `driver`.
120
+* `GET /containers/json` now supports filtering containers by `network` name.
120 121
 * `POST /containers/create` now takes `MaximumIOps` and `MaximumIOBps` fields. Windows daemon only.
121 122
 * `POST /containers/create` now returns an HTTP 400 "bad parameter" message
122 123
   if no command is specified (instead of an HTTP 500 "server error")
... ...
@@ -223,6 +223,7 @@ Query Parameters:
223 223
   -   `before`=(`<container id>` or `<container name>`)
224 224
   -   `since`=(`<container id>` or `<container name>`)
225 225
   -   `volume`=(`<volume name>` or `<mount point destination>`)
226
+  -   `network`=(`<network name>`)
226 227
 
227 228
 Status Codes:
228 229
 
... ...
@@ -62,7 +62,7 @@ The currently supported filters are:
62 62
 * since (container's id or name) - filters containers created since given id or name
63 63
 * isolation (default|process|hyperv)   (Windows daemon only)
64 64
 * volume (volume name or mount point) - filters containers that mount volumes.
65
-
65
+* network (network name) - filters containers connected to the provided network name
66 66
 
67 67
 #### Label
68 68
 
... ...
@@ -207,6 +207,17 @@ The `volume` filter shows only containers that mount a specific volume or have a
207 207
     CONTAINER ID        MOUNTS
208 208
     9c3527ed70ce        remote-volume
209 209
 
210
+#### Network
211
+
212
+The `network` filter shows only containers that has endpoints on the provided network name.
213
+
214
+    $docker run -d --net=net1 --name=test1 ubuntu top
215
+    $docker run -d --net=net2 --name=test2 ubuntu top
216
+
217
+    $docker ps --filter network=net1
218
+    CONTAINER ID        IMAGE       COMMAND       CREATED             STATUS              PORTS               NAMES
219
+    9d4893ed80fe        ubuntu      "top"         10 minutes ago      Up 10 minutes                           test1
220
+
210 221
 
211 222
 ## Formatting
212 223
 
... ...
@@ -804,3 +804,40 @@ func (s *DockerSuite) TestPsFormatSize(c *check.C) {
804 804
 	lines = strings.Split(out, "\n")
805 805
 	c.Assert(lines[8], checker.HasPrefix, "size:", check.Commentf("Size should be appended on a newline"))
806 806
 }
807
+
808
+func (s *DockerSuite) TestPsListContainersFilterNetwork(c *check.C) {
809
+	// create a container
810
+	out, _ := runSleepingContainer(c, "--net=bridge", "--name=onbridgenetwork")
811
+	out, _ = runSleepingContainer(c, "--net=none", "--name=onnonenetwork")
812
+
813
+	// Filter docker ps on network bridge
814
+	out, _ = dockerCmd(c, "ps", "--filter", "network=bridge")
815
+	containerOut := strings.TrimSpace(string(out))
816
+
817
+	lines := strings.Split(containerOut, "\n")
818
+
819
+	// skip header
820
+	lines = lines[1:]
821
+
822
+	// ps output should have only one container
823
+	c.Assert(lines, checker.HasLen, 1)
824
+
825
+	// Making sure onbridgenetwork is on the output
826
+	c.Assert(lines[0], checker.Contains, "onbridgenetwork", check.Commentf("Missing the container on network\n"))
827
+
828
+	// Filter docker ps on networks bridge and none
829
+	out, _ = dockerCmd(c, "ps", "--filter", "network=bridge", "--filter", "network=none")
830
+	containerOut = strings.TrimSpace(string(out))
831
+
832
+	lines = strings.Split(containerOut, "\n")
833
+
834
+	// skip header
835
+	lines = lines[1:]
836
+
837
+	//ps output should have both the containers
838
+	c.Assert(lines, checker.HasLen, 2)
839
+
840
+	// Making sure onbridgenetwork and onnonenetwork is on the output
841
+	c.Assert(lines[0], checker.Contains, "onnonenetwork", check.Commentf("Missing the container on none network\n"))
842
+	c.Assert(lines[1], checker.Contains, "onbridgenetwork", check.Commentf("Missing the container on bridge network\n"))
843
+}
... ...
@@ -36,6 +36,7 @@ the running containers.
36 36
    - since=(<container-name>|<container-id>)
37 37
    - ancestor=(<image-name>[:tag]|<image-id>|<image@digest>) - containers created from an image or a descendant.
38 38
    - volume=(<volume-name>|<mount-point-destination>)
39
+   - network=(<network-name>) - containers connected to the provided network name
39 40
 
40 41
 **--format**="*TEMPLATE*"
41 42
    Pretty-print containers using a Go template.