Browse code

Add containers’ networks to /containers/json

After addition of multi-host networking in Docker 1.9, Docker Remote
API is still returning only the network specified during creation
of the container in the “List Containers” (`/containers/json`) endpoint:

...
"HostConfig": {
"NetworkMode": "default"
},

The list of networks containers are attached to is only available at
Get Container (`/containers/<id>/json`) endpoint.
This does not allow applications utilizing multi-host networking to
be built on top of Docker Remote API.

Therefore I added a simple `"NetworkSettings"` section to the
`/containers/json` endpoint. This is not identical to the NetworkSettings
returned in Get Container (`/containers/<id>/json`) endpoint. It only
contains a single field `"Networks"`, which is essentially the same
value shown in inspect output of a container.

This change adds the following section to the `/containers/json`:

"NetworkSettings": {
"Networks": {
"bridge": {
"EndpointID": "2cdc4edb1ded3631c81f57966563e...",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02"
}
}
}

This is of type `SummaryNetworkSettings` type, a minimal version of
`api/types#NetworkSettings`.

Actually all I need is the network name and the IPAddress fields. If folks
find this addition too big, I can create a `SummaryEndpointSettings` field
as well, containing just the IPAddress field.

Signed-off-by: Ahmet Alp Balkan <ahmetalpbalkan@gmail.com>

Ahmet Alp Balkan authored on 2015/12/10 13:48:50
Showing 5 changed files
... ...
@@ -140,6 +140,7 @@ type Container struct {
140 140
 	HostConfig struct {
141 141
 		NetworkMode string `json:",omitempty"`
142 142
 	}
143
+	NetworkSettings *SummaryNetworkSettings
143 144
 }
144 145
 
145 146
 // CopyConfig contains request body of Remote API:
... ...
@@ -303,6 +304,12 @@ type NetworkSettings struct {
303 303
 	Networks map[string]*network.EndpointSettings
304 304
 }
305 305
 
306
+// SummaryNetworkSettings provides a summary of container's networks
307
+// in /containers/json
308
+type SummaryNetworkSettings struct {
309
+	Networks map[string]*network.EndpointSettings
310
+}
311
+
306 312
 // NetworkSettingsBase holds basic information about networks
307 313
 type NetworkSettingsBase struct {
308 314
 	Bridge                 string
... ...
@@ -351,6 +351,7 @@ func (daemon *Daemon) transformContainer(container *container.Container, ctx *li
351 351
 	newC.Created = container.Created.Unix()
352 352
 	newC.Status = container.State.String()
353 353
 	newC.HostConfig.NetworkMode = string(container.HostConfig.NetworkMode)
354
+	newC.NetworkSettings = &types.SummaryNetworkSettings{container.NetworkSettings.Networks}
354 355
 
355 356
 	newC.Ports = []types.Port{}
356 357
 	for port, bindings := range container.NetworkSettings.Ports {
... ...
@@ -96,6 +96,7 @@ This section lists each version from latest to oldest.  Each listing includes a
96 96
 [Docker Remote API v1.22](docker_remote_api_v1.22.md) documentation
97 97
 
98 98
 * `GET /containers/json` supports filter `isolation` on Windows.
99
+* `GET /containers/json` now returns the list of networks of containers.
99 100
 * `GET /info` Now returns `Architecture` and `OSType` fields, providing information
100 101
   about the host architecture and operating system type that the daemon runs on.
101 102
 * `GET /networks/(name)` now returns a `Name` field for each container attached to the network.
... ...
@@ -57,7 +57,21 @@ List containers
57 57
                          "com.example.version": "1.0"
58 58
                  },
59 59
                  "SizeRw": 12288,
60
-                 "SizeRootFs": 0
60
+                 "SizeRootFs": 0,
61
+                 "NetworkSettings": {
62
+                         "Networks": {
63
+                                 "bridge": {
64
+                                          "EndpointID": "2cdc4edb1ded3631c81f57966563e5c8525b81121bb3706a9a9a3ae102711f3f",
65
+                                          "Gateway": "172.17.0.1",
66
+                                          "IPAddress": "172.17.0.2",
67
+                                          "IPPrefixLen": 16,
68
+                                          "IPv6Gateway": "",
69
+                                          "GlobalIPv6Address": "",
70
+                                          "GlobalIPv6PrefixLen": 0,
71
+                                          "MacAddress": "02:42:ac:11:00:02"
72
+                                  }
73
+                         }
74
+                 }
61 75
          },
62 76
          {
63 77
                  "Id": "9cd87474be90",
... ...
@@ -70,7 +84,22 @@ List containers
70 70
                  "Ports": [],
71 71
                  "Labels": {},
72 72
                  "SizeRw": 12288,
73
-                 "SizeRootFs": 0
73
+                 "SizeRootFs": 0,
74
+                 "NetworkSettings": {
75
+                         "Networks": {
76
+                                 "bridge": {
77
+                                          "EndpointID": "88eaed7b37b38c2a3f0c4bc796494fdf51b270c2d22656412a2ca5d559a64d7a",
78
+                                          "Gateway": "172.17.0.1",
79
+                                          "IPAddress": "172.17.0.8",
80
+                                          "IPPrefixLen": 16,
81
+                                          "IPv6Gateway": "",
82
+                                          "GlobalIPv6Address": "",
83
+                                          "GlobalIPv6PrefixLen": 0,
84
+                                          "MacAddress": "02:42:ac:11:00:08"
85
+                                  }
86
+                         }
87
+                 }
88
+
74 89
          },
75 90
          {
76 91
                  "Id": "3176a2479c92",
... ...
@@ -83,7 +112,22 @@ List containers
83 83
                  "Ports":[],
84 84
                  "Labels": {},
85 85
                  "SizeRw":12288,
86
-                 "SizeRootFs":0
86
+                 "SizeRootFs":0,
87
+                 "NetworkSettings": {
88
+                         "Networks": {
89
+                                 "bridge": {
90
+                                          "EndpointID": "8b27c041c30326d59cd6e6f510d4f8d1d570a228466f956edf7815508f78e30d",
91
+                                          "Gateway": "172.17.0.1",
92
+                                          "IPAddress": "172.17.0.6",
93
+                                          "IPPrefixLen": 16,
94
+                                          "IPv6Gateway": "",
95
+                                          "GlobalIPv6Address": "",
96
+                                          "GlobalIPv6PrefixLen": 0,
97
+                                          "MacAddress": "02:42:ac:11:00:06"
98
+                                  }
99
+                         }
100
+                 }
101
+
87 102
          },
88 103
          {
89 104
                  "Id": "4cb07b47f9fb",
... ...
@@ -96,7 +140,22 @@ List containers
96 96
                  "Ports": [],
97 97
                  "Labels": {},
98 98
                  "SizeRw": 12288,
99
-                 "SizeRootFs": 0
99
+                 "SizeRootFs": 0,
100
+                 "NetworkSettings": {
101
+                         "Networks": {
102
+                                 "bridge": {
103
+                                          "EndpointID": "d91c7b2f0644403d7ef3095985ea0e2370325cd2332ff3a3225c4247328e66e9",
104
+                                          "Gateway": "172.17.0.1",
105
+                                          "IPAddress": "172.17.0.5",
106
+                                          "IPPrefixLen": 16,
107
+                                          "IPv6Gateway": "",
108
+                                          "GlobalIPv6Address": "",
109
+                                          "GlobalIPv6PrefixLen": 0,
110
+                                          "MacAddress": "02:42:ac:11:00:05"
111
+                                  }
112
+                         }
113
+                 }
114
+
100 115
          }
101 116
     ]
102 117
 
... ...
@@ -68,6 +68,7 @@ func (s *DockerSuite) TestContainerApiGetJSONNoFieldsOmitted(c *check.C) {
68 68
 		"Ports",
69 69
 		"Labels",
70 70
 		"Status",
71
+		"NetworkSettings",
71 72
 	}
72 73
 
73 74
 	// decoding into types.Container do not work since it eventually unmarshal