5

I'm trying to get Docker networks list and filter it by name using Docker HTTP API 1.24.

For some reason, filters are extremly poorly documented, the very single example in whole Docker documentation is this one (and it works):

GET /networks?filters={"type":{"custom":true}} HTTP/1.1

What is confusing, this lonely example seems to contradict official Docker notation for filters:

map[string][]string

which is discussed here: https://stackoverflow.com/a/28055631/4486909

API spec didn't do much help either:

Query parameters:
  filters - JSON encoded network list filter. The filter value is one of:
  driver=<driver-name> Matches a network’s driver.
  id=<network-id> Matches all or part of a network id.
  label=<key> or label=<key>=<value> of a network label.
  name=<network-name> Matches all or part of a network name.
  type=["custom"|"builtin"] Filters networks by type.

http://docs.master.dockerproject.org/engine/reference/api/docker_remote_api_v1.24/#/list-networks


These ones don't filter our anything, returning full list of netwroks.

curl -vg -X GET "v1.24/networks" --data-urlencode 'filters={"name":["MyNetwork"]}'

curl -vg -X GET "v1.24/networks" --data-urlencode 'filters={"name":["MyNetwork":true]}'

curl -vg -X GET "v1.24/networks" --data-urlencode 'filters={"name":"MyNetwork"}'

What am I doing wrong? Many thanks.

Community
  • 1
  • 1
Olesya Bolobova
  • 1,573
  • 1
  • 10
  • 21

2 Answers2

8

Thanks to BMitch for valuable hint to Docker debug mode, which allows to trace incoming HTTP requests.

Some notes about curl params for GET requests to Docker:

curl -gG -XGET "v1.24/networks" --data-urlencode 'filters={"name":{"MyNetwork":true}}'
-g = disable globbing, necessary for JSON proper proccessing
-G = include --data-urlencode contents into GET
Community
  • 1
  • 1
Olesya Bolobova
  • 1,573
  • 1
  • 10
  • 21
5

I tested locally by putting my server into debug mode and running a:

docker network ls -f name=MyNetwork

The resulting filter in the logs was:

filters={"name":{"MyNetwork":true}}"

Encoded, it looked like:

/v1.25/networks?filters=%7B%22name%22%3A%7B%22MyNetwork%22%3Atrue%7D%7D"
BMitch
  • 231,797
  • 42
  • 475
  • 450