|
1 | 1 | # Using Networks
|
2 | 2 |
|
| 3 | +## Network creation |
| 4 | + |
3 | 5 | With the release of Docker 1.9 you can now manage custom networks.
|
4 | 6 |
|
5 | 7 |
|
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 |
7 | 10 |
|
8 | 11 | ```python
|
9 | 12 | docker_client.create_network("network1", driver="bridge")
|
10 | 13 | ```
|
11 | 14 |
|
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` |
14 | 18 |
|
15 | 19 | ```python
|
16 |
| - |
17 |
| -ipam_config = docker.utils.create_ipam_config(subnet='192.168.52.0/24', gateway='192.168.52.254') |
| 20 | +ipam_pool = docker.utils.create_ipam_pool( |
| 21 | + subnet='192.168.52.0/24', |
| 22 | + gateway='192.168.52.254' |
| 23 | +) |
| 24 | +ipam_config = docker.utils.create_ipam_config( |
| 25 | + pool_configs=[ipam_pool] |
| 26 | +) |
18 | 27 |
|
19 | 28 | docker_client.create_network("network1", driver="bridge", ipam=ipam_config)
|
20 | 29 | ```
|
21 | 30 |
|
22 |
| -With Docker 1.10 you can now also create internal networks |
| 31 | +By default, when you connect a container to an overlay network, Docker also |
| 32 | +connects a bridge network to it to provide external connectivity. If you want |
| 33 | +to create an externally isolated overlay network, with Docker 1.10 you can |
| 34 | +create an internal network. |
23 | 35 |
|
24 | 36 | ```python
|
25 | 37 |
|
26 | 38 | docker_client.create_network("network1", driver="bridge", internal=True)
|
27 | 39 | ```
|
| 40 | + |
| 41 | +## Container network configuration |
| 42 | + |
| 43 | +In order to specify which network a container will be connected to, and |
| 44 | +additional configuration, use the `networking_config` parameter in |
| 45 | +`Client.create_container`. Note that at the time of creation, you can |
| 46 | +only connect a container to a single network. Later on, you may create more |
| 47 | +connections using `Client.connect_container_to_network`. |
| 48 | + |
| 49 | + |
| 50 | +```python |
| 51 | +networking_config = docker_client.create_networking_config({ |
| 52 | + 'network1': docker_client.create_endpoint_config( |
| 53 | + ipv4_address='172.28.0.124', |
| 54 | + aliases=['foo', 'bar'], |
| 55 | + links=['container2'] |
| 56 | + ) |
| 57 | +}) |
| 58 | + |
| 59 | +ctnr = docker_client.create_container( |
| 60 | + img, command, networking_config=networking_config |
| 61 | +) |
| 62 | + |
| 63 | +``` |
| 64 | + |
| 65 | +## Network API documentation |
| 66 | + |
| 67 | +### Client.create_networking_config |
| 68 | + |
| 69 | +Create a networking config dictionary to be used as the `networking_config` |
| 70 | +parameter in `Client.create_container_config` |
| 71 | + |
| 72 | +**Params**: |
| 73 | + |
| 74 | +* endpoints_config (dict): A dictionary of `network_name -> endpoint_config` |
| 75 | + relationships. Values should be endpoint config dictionaries created by |
| 76 | + `Client.create_endpoint_config`. Defaults to `None` (default config). |
| 77 | + |
| 78 | +**Returns** A networking config dictionary. |
| 79 | + |
| 80 | +```python |
| 81 | + |
| 82 | +docker_client.create_network('network1') |
| 83 | + |
| 84 | +networking_config = docker_client.create_networking_config({ |
| 85 | + 'network1': docker_client.create_endpoint_config() |
| 86 | +}) |
| 87 | + |
| 88 | +container = docker_client.create_container( |
| 89 | + img, command, networking_config=networking_config |
| 90 | +) |
| 91 | +``` |
| 92 | + |
| 93 | + |
| 94 | +### Client.create_endpoint_config |
| 95 | + |
| 96 | +Create an endpoint config dictionary to be used with |
| 97 | +`Client.create_networking_config`. |
| 98 | + |
| 99 | +**Params**: |
| 100 | + |
| 101 | +* aliases (list): A list of aliases for this endpoint. Names in that list can |
| 102 | + be used within the network to reach the container. Defaults to `None`. |
| 103 | +* links (list): A list of links for this endpoint. Containers declared in this |
| 104 | + list will be [linked](https://docs.docker.com/engine/userguide/networking/work-with-networks/#linking-containers-in-user-defined-networks) |
| 105 | + to this container. Defaults to `None`. |
| 106 | +* ipv4_address (str): The IP address of this container on the network, |
| 107 | + using the IPv4 protocol. Defaults to `None`. |
| 108 | +* ipv6_address (str): The IP address of this container on the network, |
| 109 | + using the IPv6 protocol. Defaults to `None`. |
| 110 | + |
| 111 | +**Returns** An endpoint config dictionary. |
| 112 | + |
| 113 | +```python |
| 114 | +endpoint_config = docker_client.create_endpoint_config( |
| 115 | + aliases=['web', 'app'], |
| 116 | + links=['app_db'], |
| 117 | + ipv4_address='132.65.0.123' |
| 118 | +) |
| 119 | + |
| 120 | +docker_client.create_network('network1') |
| 121 | +networking_config = docker_client.create_networking_config({ |
| 122 | + 'network1': endpoint_config |
| 123 | +}) |
| 124 | +container = docker_client.create_container( |
| 125 | + img, command, networking_config=networking_config |
| 126 | +) |
| 127 | +``` |
| 128 | +### docker.utils.create_ipam_config |
| 129 | + |
| 130 | +Create an IPAM (IP Address Management) config dictionary to be used with |
| 131 | +`Client.create_network`. |
| 132 | + |
| 133 | + |
| 134 | +**Params**: |
| 135 | + |
| 136 | +* driver (str): The IPAM driver to use. Defaults to `'default'`. |
| 137 | +* pool_configs (list): A list of pool configuration dictionaries as created |
| 138 | + by `docker.utils.create_ipam_pool`. Defaults to empty list. |
| 139 | + |
| 140 | +**Returns** An IPAM config dictionary |
| 141 | + |
| 142 | +```python |
| 143 | +ipam_config = docker.utils.create_ipam_config(driver='default') |
| 144 | +network = docker_client.create_network('network1', ipam=ipam_config) |
| 145 | +``` |
| 146 | + |
| 147 | +### docker.utils.create_ipam_pool |
| 148 | + |
| 149 | +Create an IPAM pool config dictionary to be added to the `pool_configs` param |
| 150 | +in `docker.utils.create_ipam_config`. |
| 151 | + |
| 152 | +**Params**: |
| 153 | + |
| 154 | +* subnet (str): Custom subnet for this IPAM pool using the CIDR notation. |
| 155 | + Defaults to `None`. |
| 156 | +* iprange (str): Custom IP range for endpoints in this IPAM pool using the |
| 157 | + CIDR notation. Defaults to `None`. |
| 158 | +* gateway (str): Custom IP address for the pool's gateway. |
| 159 | +* aux_addresses (dict): A dictionary of `key -> ip_address` relationships |
| 160 | + specifying auxiliary addresses that need to be allocated by the |
| 161 | + IPAM driver. |
| 162 | + |
| 163 | +**Returns** An IPAM pool config dictionary |
| 164 | + |
| 165 | +```python |
| 166 | +ipam_pool = docker.utils.create_ipam_pool( |
| 167 | + subnet='124.42.0.0/16', |
| 168 | + iprange='124.42.0.0/24', |
| 169 | + gateway='124.42.0.254', |
| 170 | + aux_addresses={ |
| 171 | + 'reserved1': '124.42.1.1' |
| 172 | + } |
| 173 | +) |
| 174 | +ipam_config = docker.utils.create_ipam_config(pool_configs=[ipam_pool]) |
| 175 | +network = docker_client.create_network('network1', ipam=ipam_config) |
| 176 | +``` |
0 commit comments