0
# Network Management
1
2
Docker network creation, management, and container connectivity operations. Networks provide isolated communication channels for containers and enable service discovery within Docker environments.
3
4
## Capabilities
5
6
### Network Creation and Configuration
7
8
Create custom networks with specific drivers and configuration options.
9
10
```python { .api }
11
def create(
12
name: str,
13
*,
14
driver: Optional[str] = None,
15
options: Optional[Dict[str, str]] = None,
16
ipam_driver: Optional[str] = None,
17
ipam_config: Optional[List[Dict[str, str]]] = None,
18
enable_ipv6: bool = False,
19
internal: bool = False,
20
labels: Optional[Dict[str, str]] = None,
21
scope: Optional[str] = None,
22
attachable: bool = False,
23
ingress: bool = False,
24
config_only: bool = False,
25
config_from: Optional[str] = None
26
) -> Network:
27
"""
28
Create a new network.
29
30
Parameters:
31
- name: Network name
32
- driver: Network driver (bridge, overlay, host, none, macvlan)
33
- options: Driver-specific options
34
- ipam_driver: IPAM driver name
35
- ipam_config: IPAM configuration (subnet, gateway, ip_range, aux_addresses)
36
- enable_ipv6: Enable IPv6 networking
37
- internal: Restrict external access to network
38
- labels: Network labels
39
- scope: Network scope (local, global, swarm)
40
- attachable: Enable manual container attachment
41
- ingress: Create swarm routing-mesh network
42
- config_only: Create configuration-only network
43
- config_from: Network to copy configuration from
44
45
Returns:
46
- Network object
47
"""
48
```
49
50
### Network Listing and Inspection
51
52
List and inspect networks with filtering and detailed information retrieval.
53
54
```python { .api }
55
def list(
56
filters: Optional[Dict[str, str]] = None,
57
*,
58
quiet: bool = False,
59
no_trunc: bool = False
60
) -> List[Network]:
61
"""
62
List networks.
63
64
Parameters:
65
- filters: Filters to apply (driver, name, id, label, scope, type)
66
- quiet: Only show network IDs
67
- no_trunc: Don't truncate output
68
69
Returns:
70
- List of Network objects
71
"""
72
73
def inspect(network: str) -> Network:
74
"""
75
Get detailed information about a network.
76
77
Parameters:
78
- network: Network name or ID
79
80
Returns:
81
- Network object with full details
82
"""
83
```
84
85
### Container Network Connectivity
86
87
Connect and disconnect containers from networks.
88
89
```python { .api }
90
def connect(
91
network: str,
92
container: str,
93
*,
94
aliases: Optional[List[str]] = None,
95
ip: Optional[str] = None,
96
ipv6_address: Optional[str] = None,
97
links: Optional[List[str]] = None,
98
driver_options: Optional[Dict[str, str]] = None
99
) -> None:
100
"""
101
Connect container to network.
102
103
Parameters:
104
- network: Network name or ID
105
- container: Container name or ID
106
- aliases: Network-scoped aliases for container
107
- ip: IPv4 address for container
108
- ipv6_address: IPv6 address for container
109
- links: Container links (deprecated)
110
- driver_options: Driver-specific options
111
"""
112
113
def disconnect(
114
network: str,
115
container: str,
116
*,
117
force: bool = False
118
) -> None:
119
"""
120
Disconnect container from network.
121
122
Parameters:
123
- network: Network name or ID
124
- container: Container name or ID
125
- force: Force disconnect
126
"""
127
```
128
129
### Network Cleanup
130
131
Remove networks and clean up unused networks.
132
133
```python { .api }
134
def remove(networks: Union[str, List[str]]) -> None:
135
"""
136
Remove networks.
137
138
Parameters:
139
- networks: Network name(s) or ID(s)
140
"""
141
142
def prune(filters: Optional[Dict[str, str]] = None) -> Dict[str, Any]:
143
"""
144
Remove unused networks.
145
146
Parameters:
147
- filters: Filters to apply
148
149
Returns:
150
- Information about removed networks
151
"""
152
```
153
154
## Usage Examples
155
156
### Basic Network Operations
157
158
```python
159
from python_on_whales import docker
160
161
# Create a custom bridge network
162
network = docker.network.create(
163
"my-app-network",
164
driver="bridge",
165
labels={"project": "my-app"}
166
)
167
168
# List all networks
169
networks = docker.network.list()
170
for net in networks:
171
print(f"Network: {net.name} - Driver: {net.driver}")
172
173
# Connect container to network
174
docker.network.connect("my-app-network", "my-container")
175
176
# Disconnect and remove
177
docker.network.disconnect("my-app-network", "my-container")
178
docker.network.remove("my-app-network")
179
```
180
181
### Advanced Network Configuration
182
183
```python
184
# Create overlay network for swarm
185
overlay_network = docker.network.create(
186
"my-overlay",
187
driver="overlay",
188
attachable=True,
189
ipam_config=[{
190
"subnet": "10.0.0.0/24",
191
"gateway": "10.0.0.1"
192
}]
193
)
194
195
# Create macvlan network
196
macvlan_network = docker.network.create(
197
"my-macvlan",
198
driver="macvlan",
199
options={
200
"parent": "eth0"
201
},
202
ipam_config=[{
203
"subnet": "192.168.1.0/24",
204
"gateway": "192.168.1.1",
205
"ip_range": "192.168.1.128/25"
206
}]
207
)
208
```
209
210
## Types
211
212
```python { .api }
213
class Network:
214
id: str
215
name: str
216
driver: str
217
scope: str
218
created: str
219
internal: bool
220
attachable: bool
221
ingress: bool
222
ipam: Dict[str, Any]
223
containers: Dict[str, Dict[str, Any]]
224
options: Dict[str, str]
225
labels: Dict[str, str]
226
227
def remove(self) -> None: ...
228
def connect(self, container: str, **kwargs) -> None: ...
229
def disconnect(self, container: str, force: bool = False) -> None: ...
230
def reload(self) -> None: ...
231
```