Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Remote API filters: filter out network by name

Tags:

docker

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.

like image 287
Olesya Bolobova Avatar asked Jan 31 '17 15:01

Olesya Bolobova


2 Answers

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
like image 124
Olesya Bolobova Avatar answered Sep 28 '22 19:09

Olesya Bolobova


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"
like image 40
BMitch Avatar answered Sep 28 '22 17:09

BMitch