Skip to content

Commit b6fa986

Browse files
committed
Add ipv[46]_address params to create_endpoint_config.
Update networks documentation with exhaustive API docs Signed-off-by: Joffrey F <[email protected]>
1 parent 299ffad commit b6fa986

File tree

4 files changed

+226
-11
lines changed

4 files changed

+226
-11
lines changed

docker/utils/utils.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -813,19 +813,30 @@ def create_networking_config(endpoints_config=None):
813813
return networking_config
814814

815815

816-
def create_endpoint_config(version, aliases=None, links=None):
816+
def create_endpoint_config(version, aliases=None, links=None,
817+
ipv4_address=None, ipv6_address=None):
818+
if version_lt(version, '1.22'):
819+
raise errors.InvalidVersion(
820+
'Endpoint config is not supported for API version < 1.22'
821+
)
817822
endpoint_config = {}
818823

819824
if aliases:
820-
if version_lt(version, '1.22'):
821-
raise host_config_version_error('endpoint_config.aliases', '1.22')
822825
endpoint_config["Aliases"] = aliases
823826

824827
if links:
825-
if version_lt(version, '1.22'):
826-
raise host_config_version_error('endpoint_config.links', '1.22')
827828
endpoint_config["Links"] = normalize_links(links)
828829

830+
ipam_config = {}
831+
if ipv4_address:
832+
ipam_config['IPv4Address'] = ipv4_address
833+
834+
if ipv6_address:
835+
ipam_config['IPv6Address'] = ipv6_address
836+
837+
if ipam_config:
838+
endpoint_config['IPAMConfig'] = ipam_config
839+
829840
return endpoint_config
830841

831842

docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ from. Optionally a single string joining container id's with commas
242242
* labels (dict or list): A dictionary of name-value labels (e.g. `{"label1": "value1", "label2": "value2"}`) or a list of names of labels to set with empty values (e.g. `["label1", "label2"]`)
243243
* volume_driver (str): The name of a volume driver/plugin.
244244
* stop_signal (str): The stop signal to use to stop the container (e.g. `SIGINT`).
245+
* networking_config (dict): A [NetworkingConfig](networks.md) dictionary
245246

246247
**Returns** (dict): A dictionary with an image 'Id' key and a 'Warnings' key.
247248

docs/networks.md

Lines changed: 149 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,171 @@
11
# Using Networks
22

3+
## Network creation
4+
35
With the release of Docker 1.9 you can now manage custom networks.
46

57

6-
Here you can see how to create a network named ```network1``` using the ```bridge``` driver
8+
Here you can see how to create a network named `network1` using
9+
the `bridge` driver
710

811
```python
912
docker_client.create_network("network1", driver="bridge")
1013
```
1114

12-
You can also create more advanced networks with custom IPAM configurations. For example,
13-
setting the subnet to ```192.168.52.0/24``` and gateway to ```192.168.52.254```
15+
You can also create more advanced networks with custom IPAM configurations.
16+
For example, setting the subnet to `192.168.52.0/24` and gateway address
17+
to `192.168.52.254`
1418

1519
```python
1620

17-
ipam_config = docker.utils.create_ipam_config(subnet='192.168.52.0/24', gateway='192.168.52.254')
21+
ipam_config = docker.utils.create_ipam_config(
22+
subnet='192.168.52.0/24',
23+
gateway='192.168.52.254'
24+
)
1825

1926
docker_client.create_network("network1", driver="bridge", ipam=ipam_config)
2027
```
2128

22-
With Docker 1.10 you can now also create internal networks
29+
By default, when you connect a container to an overlay network, Docker also
30+
connects a bridge network to it to provide external connectivity. If you want
31+
to create an externally isolated overlay network, with Docker 1.10 you can
32+
create an internal network.
2333

2434
```python
2535

2636
docker_client.create_network("network1", driver="bridge", internal=True)
2737
```
38+
39+
## Container network configuration
40+
41+
In order to specify which network(s) a container will be connected to and
42+
additional configuration, use the `networking_config` parameter in
43+
`Client.create_container`
44+
45+
```python
46+
networking_config = docker_client.create_networking_config({
47+
'network1': docker_client.create_endpoint_config(
48+
ipv4_address='172.28.0.124',
49+
aliases=['foo', 'bar'],
50+
links=['container2']
51+
)
52+
})
53+
54+
ctnr = docker_client.create_container(
55+
img, command, networking_config=networking_config
56+
)
57+
58+
```
59+
60+
## Network API documentation
61+
62+
### Client.create_networking_config
63+
64+
Create a networking config dictionary to be used as the `networking_config`
65+
parameter in `Client.create_container_config`
66+
67+
**Params**:
68+
69+
* endpoints_config (dict): A dictionary of `network_name -> endpoint_config`
70+
relationships. Values should be endpoint config dictionaries created by
71+
`Client.create_endpoint_config`. Defaults to `None` (default config).
72+
73+
**Returns** A networking config dictionary.
74+
75+
```python
76+
77+
docker_client.create_network('network1')
78+
79+
networking_config = docker_client.create_networking_config({
80+
'network1': docker_client.create_endpoint_config()
81+
})
82+
83+
container = docker_client.create_container(
84+
img, command, networking_config=networking_config
85+
)
86+
```
87+
88+
89+
### Client.create_endpoint_config
90+
91+
Create an endpoint config dictionary to be used with
92+
`Client.create_networking_config`.
93+
94+
**Params**:
95+
96+
* aliases (list): A list of aliases for this endpoint. Names in that list can
97+
be used within the network to reach the container. Defaults to `None`.
98+
* links (list): A list of links for this endpoint. Containers declared in this
99+
list will be [linked](https://docs.docker.com/engine/userguide/networking/work-with-networks/#linking-containers-in-user-defined-networks)
100+
to this container. Defaults to `None`.
101+
* ipv4_address (str): The IP address of this container on the network,
102+
using the IPv4 protocol. Defaults to `None`.
103+
* ipv6_address (str): The IP address of this container on the network,
104+
using the IPv6 protocol. Defaults to `None`.
105+
106+
**Returns** An endpoint config dictionary.
107+
108+
```python
109+
endpoint_config = docker_client.create_endpoint_config(
110+
aliases=['web', 'app'],
111+
links=['app_db'],
112+
ipv4_address='132.65.0.123'
113+
)
114+
115+
docker_client.create_network('network1')
116+
networking_config = docker_client.create_networking_config({
117+
'network1': endpoint_config
118+
})
119+
container = docker_client.create_container(
120+
img, command, networking_config=networking_config
121+
)
122+
```
123+
### docker.utils.create_ipam_config
124+
125+
Create an IPAM (IP Address Management) config dictionary to be used with
126+
`Client.create_network`.
127+
128+
129+
**Params**:
130+
131+
* driver (str): The IPAM driver to use. Defaults to `'default'`.
132+
* pool_configs (list): A list of pool configuration dictionaries as created
133+
by `docker.utils.create_ipam_pool`. Defaults to empty list.
134+
135+
**Returns** An IPAM config dictionary
136+
137+
```python
138+
ipam_config = docker.utils.create_ipam_config(driver='default')
139+
network = docker_client.create_network('network1', ipam=ipam_config)
140+
```
141+
142+
### docker.utils.create_ipam_pool
143+
144+
Create an IPAM pool config dictionary to be added to the `pool_configs` param
145+
in `docker.utils.create_ipam_config`.
146+
147+
**Params**:
148+
149+
* subnet (str): Custom subnet for this IPAM pool using the CIDR notation.
150+
Defaults to `None`.
151+
* iprange (str): Custom IP range for endpoints in this IPAM pool using the
152+
CIDR notation. Defaults to `None`.
153+
* gateway (str): Custom IP address for the pool's gateway.
154+
* aux_addresses (dict): A dictionary of `key -> ip_address` relationships
155+
specifying auxiliary addresses that need to be allocated by the
156+
IPAM driver.
157+
158+
**Returns** An IPAM pool config dictionary
159+
160+
```python
161+
ipam_pool = docker.utils.create_ipam_pool(
162+
subnet='124.42.0.0/16',
163+
iprange='124.42.0.0/8',
164+
gateway='124.42.0.254',
165+
aux_addresses={
166+
'reserved1': '124.42.1.1'
167+
}
168+
)
169+
ipam_config = docker.utils.create_ipam_config(pool_configs=[ipam_pool])
170+
network = docker_client.create_network('network1', ipam=ipam_config)
171+
```

tests/integration/network_test.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,66 @@ def test_create_with_aliases(self):
185185
container_data = self.client.inspect_container(container)
186186
self.assertEqual(
187187
container_data['NetworkSettings']['Networks'][net_name]['Aliases'],
188-
['foo', 'bar'])
188+
['foo', 'bar']
189+
)
190+
191+
@requires_api_version('1.22')
192+
def test_create_with_ipv4_address(self):
193+
net_name, net_id = self.create_network(
194+
ipam=create_ipam_config(
195+
driver='default',
196+
pool_configs=[create_ipam_pool(subnet="132.124.0.0/16")],
197+
),
198+
)
199+
container = self.client.create_container(
200+
image='busybox', command='top',
201+
host_config=self.client.create_host_config(network_mode=net_name),
202+
networking_config=self.client.create_networking_config({
203+
net_name: self.client.create_endpoint_config(
204+
ipv4_address='132.124.0.23'
205+
)
206+
})
207+
)
208+
self.tmp_containers.append(container)
209+
self.client.start(container)
210+
211+
container_data = self.client.inspect_container(container)
212+
self.assertEqual(
213+
container_data[
214+
'NetworkSettings']['Networks'][net_name]['IPAMConfig'][
215+
'IPv4Address'
216+
],
217+
'132.124.0.23'
218+
)
219+
220+
@requires_api_version('1.22')
221+
def test_create_with_ipv6_address(self):
222+
net_name, net_id = self.create_network(
223+
ipam=create_ipam_config(
224+
driver='default',
225+
pool_configs=[create_ipam_pool(subnet="2001:389::1/64")],
226+
),
227+
)
228+
container = self.client.create_container(
229+
image='busybox', command='top',
230+
host_config=self.client.create_host_config(network_mode=net_name),
231+
networking_config=self.client.create_networking_config({
232+
net_name: self.client.create_endpoint_config(
233+
ipv6_address='2001:389::f00d'
234+
)
235+
})
236+
)
237+
self.tmp_containers.append(container)
238+
self.client.start(container)
239+
240+
container_data = self.client.inspect_container(container)
241+
self.assertEqual(
242+
container_data[
243+
'NetworkSettings']['Networks'][net_name]['IPAMConfig'][
244+
'IPv6Address'
245+
],
246+
'2001:389::f00d'
247+
)
189248

190249
@requires_api_version('1.22')
191250
def test_create_with_links(self):

0 commit comments

Comments
 (0)