or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

containers.mdimages.mdindex.mdnetworking.mdswarm.mdutilities.md

networking.mddocs/

0

# Network Management

1

2

Docker network operations for creating custom networks, connecting containers, and managing network isolation. Supports bridge, overlay, and custom network drivers with advanced configuration options including IPAM (IP Address Management) and multi-host networking capabilities.

3

4

## Capabilities

5

6

### Network Operations

7

8

Core network management functions for creating, inspecting, and removing Docker networks.

9

10

```python { .api }

11

def networks(names=None, ids=None):

12

"""

13

List Docker networks.

14

15

Parameters:

16

- names (list): Filter by network names

17

- ids (list): Filter by network IDs

18

19

Returns:

20

list: List of network dictionaries with configuration details

21

"""

22

23

def create_network(name, driver=None, options=None, ipam=None,

24

check_duplicate=None, internal=False, labels=None,

25

enable_ipv6=False):

26

"""

27

Create a new Docker network.

28

29

Parameters:

30

- name (str): Network name

31

- driver (str): Network driver ('bridge', 'overlay', 'macvlan', etc.)

32

- options (dict): Driver-specific options

33

- ipam (dict): IP Address Management configuration

34

- check_duplicate (bool): Check for duplicate network names

35

- internal (bool): Restrict external access to network

36

- labels (dict): Network labels

37

- enable_ipv6 (bool): Enable IPv6 on network

38

39

Returns:

40

dict: Network creation response with 'Id' key

41

"""

42

43

def inspect_network(net_id):

44

"""

45

Get detailed information about a network.

46

47

Parameters:

48

- net_id (str): Network ID or name

49

50

Returns:

51

dict: Network configuration and state information

52

"""

53

54

def remove_network(net_id):

55

"""

56

Remove a network.

57

58

Parameters:

59

- net_id (str): Network ID or name

60

61

Returns:

62

None

63

"""

64

```

65

66

### Container Network Operations

67

68

Functions for connecting and disconnecting containers to/from networks with advanced configuration.

69

70

```python { .api }

71

def connect_container_to_network(container, net_id, ipv4_address=None,

72

ipv6_address=None, aliases=None,

73

links=None, link_local_ips=None):

74

"""

75

Connect a container to a network.

76

77

Parameters:

78

- container (str): Container ID or name

79

- net_id (str): Network ID or name

80

- ipv4_address (str): IPv4 address for container on network

81

- ipv6_address (str): IPv6 address for container on network

82

- aliases (list): Network-scoped aliases for container

83

- links (list): Links to other containers (deprecated)

84

- link_local_ips (list): Link-local IPv4/IPv6 addresses

85

86

Returns:

87

None

88

"""

89

90

def disconnect_container_from_network(container, net_id, force=False):

91

"""

92

Disconnect a container from a network.

93

94

Parameters:

95

- container (str): Container ID or name

96

- net_id (str): Network ID or name

97

- force (bool): Force disconnect

98

99

Returns:

100

None

101

"""

102

```

103

104

### Network Configuration Helpers

105

106

Utility functions for building complex network configurations.

107

108

```python { .api }

109

def create_networking_config(endpoints_config=None):

110

"""

111

Create networking configuration for container creation.

112

113

Parameters:

114

- endpoints_config (dict): Endpoint configurations keyed by network name

115

116

Returns:

117

dict: Networking configuration

118

"""

119

120

def create_endpoint_config(version, aliases=None, links=None,

121

ipv4_address=None, ipv6_address=None,

122

link_local_ips=None):

123

"""

124

Create endpoint configuration for network connection.

125

126

Parameters:

127

- version (str): API version

128

- aliases (list): Network-scoped aliases

129

- links (list): Container links (deprecated)

130

- ipv4_address (str): Static IPv4 address

131

- ipv6_address (str): Static IPv6 address

132

- link_local_ips (list): Link-local addresses

133

134

Returns:

135

dict: Endpoint configuration

136

"""

137

138

def create_ipam_config(driver='default', pool_configs=None):

139

"""

140

Create IP Address Management configuration.

141

142

Parameters:

143

- driver (str): IPAM driver name

144

- pool_configs (list): List of IPAM pool configurations

145

146

Returns:

147

dict: IPAM configuration

148

"""

149

150

def create_ipam_pool(subnet=None, iprange=None, gateway=None, aux_addresses=None):

151

"""

152

Create IPAM pool configuration.

153

154

Parameters:

155

- subnet (str): Subnet in CIDR format

156

- iprange (str): IP range in CIDR format

157

- gateway (str): Gateway IP address

158

- aux_addresses (dict): Auxiliary addresses

159

160

Returns:

161

dict: IPAM pool configuration

162

"""

163

```

164

165

## Network Drivers

166

167

Docker supports multiple network drivers for different use cases:

168

169

### Bridge Networks

170

Default driver for single-host networking with port mapping and container isolation.

171

172

### Overlay Networks

173

Multi-host networking for Docker Swarm clusters with built-in service discovery.

174

175

### Host Networks

176

Remove network isolation and use host networking stack directly.

177

178

### None Networks

179

Disable all networking for maximum isolation.

180

181

### Custom Network Drivers

182

Third-party drivers for specialized networking requirements.

183

184

## Usage Examples

185

186

### Basic Network Operations

187

188

```python

189

import docker

190

191

client = docker.Client()

192

193

# List existing networks

194

networks = client.networks()

195

for network in networks:

196

print(f"Name: {network['Name']}, Driver: {network['Driver']}")

197

198

# Create a custom bridge network

199

network = client.create_network(

200

name='my-network',

201

driver='bridge',

202

options={

203

'com.docker.network.bridge.name': 'my-bridge'

204

},

205

labels={'environment': 'development'}

206

)

207

208

print(f"Created network: {network['Id']}")

209

210

# Inspect network details

211

network_info = client.inspect_network('my-network')

212

print(f"Subnet: {network_info['IPAM']['Config'][0]['Subnet']}")

213

214

# Remove network

215

client.remove_network('my-network')

216

```

217

218

### Container Network Management

219

220

```python

221

# Create a network

222

client.create_network('app-network', driver='bridge')

223

224

# Create containers

225

web_container = client.create_container(

226

image='nginx:latest',

227

name='web-server'

228

)

229

230

db_container = client.create_container(

231

image='postgres:latest',

232

name='database',

233

environment={'POSTGRES_PASSWORD': 'secret'}

234

)

235

236

# Start containers

237

client.start(web_container)

238

client.start(db_container)

239

240

# Connect containers to network with aliases

241

client.connect_container_to_network(

242

web_container,

243

'app-network',

244

aliases=['web', 'frontend']

245

)

246

247

client.connect_container_to_network(

248

db_container,

249

'app-network',

250

aliases=['db', 'database'],

251

ipv4_address='172.20.0.100' # Static IP

252

)

253

254

# Containers can now communicate using aliases

255

# web-server can reach database at 'db' or 'database'

256

```

257

258

### Advanced Network Configuration

259

260

```python

261

# Create network with custom IPAM configuration

262

ipam_pool = client.create_ipam_pool(

263

subnet='192.168.100.0/24',

264

gateway='192.168.100.1',

265

aux_addresses={

266

'reserved1': '192.168.100.2',

267

'reserved2': '192.168.100.3'

268

}

269

)

270

271

ipam_config = client.create_ipam_config(

272

driver='default',

273

pool_configs=[ipam_pool]

274

)

275

276

network = client.create_network(

277

name='custom-network',

278

driver='bridge',

279

ipam=ipam_config,

280

options={

281

'com.docker.network.bridge.enable_icc': 'true',

282

'com.docker.network.bridge.enable_ip_masquerade': 'true',

283

'com.docker.network.driver.mtu': '1500'

284

},

285

labels={

286

'project': 'myapp',

287

'environment': 'production'

288

}

289

)

290

```

291

292

### Multi-Container Application with Networks

293

294

```python

295

# Create application network

296

client.create_network('myapp-network', driver='bridge')

297

298

# Create database container

299

db_container = client.create_container(

300

image='postgres:13',

301

name='myapp-db',

302

environment={

303

'POSTGRES_DB': 'myapp',

304

'POSTGRES_USER': 'appuser',

305

'POSTGRES_PASSWORD': 'secret'

306

},

307

volumes=['/var/lib/postgresql/data']

308

)

309

310

# Create Redis cache container

311

cache_container = client.create_container(

312

image='redis:6-alpine',

313

name='myapp-cache'

314

)

315

316

# Create web application container

317

web_container = client.create_container(

318

image='myapp:latest',

319

name='myapp-web',

320

ports=['8000/tcp'],

321

environment={

322

'DATABASE_URL': 'postgresql://appuser:secret@db:5432/myapp',

323

'REDIS_URL': 'redis://cache:6379'

324

}

325

)

326

327

# Start all containers

328

for container in [db_container, cache_container, web_container]:

329

client.start(container)

330

331

# Connect all containers to application network

332

client.connect_container_to_network(db_container, 'myapp-network', aliases=['db'])

333

client.connect_container_to_network(cache_container, 'myapp-network', aliases=['cache'])

334

client.connect_container_to_network(web_container, 'myapp-network', aliases=['web'])

335

336

# Expose web container port to host

337

host_config = client.create_host_config(

338

port_bindings={'8000/tcp': 8000}

339

)

340

341

# Web container can reach database at 'db:5432' and cache at 'cache:6379'

342

```

343

344

### Network Isolation Example

345

346

```python

347

# Create isolated networks for different environments

348

networks = ['frontend-network', 'backend-network']

349

350

for network_name in networks:

351

client.create_network(

352

name=network_name,

353

driver='bridge',

354

internal=True, # No external access

355

labels={'isolation': 'true'}

356

)

357

358

# Frontend containers (web servers, load balancers)

359

frontend_container = client.create_container(

360

image='nginx:latest',

361

name='frontend'

362

)

363

364

# Backend containers (application servers, databases)

365

backend_container = client.create_container(

366

image='myapp-backend:latest',

367

name='backend'

368

)

369

370

database_container = client.create_container(

371

image='postgres:13',

372

name='database'

373

)

374

375

# Start containers

376

for container in [frontend_container, backend_container, database_container]:

377

client.start(container)

378

379

# Connect to appropriate networks

380

client.connect_container_to_network(frontend_container, 'frontend-network')

381

client.connect_container_to_network(backend_container, 'frontend-network') # Bridge between networks

382

client.connect_container_to_network(backend_container, 'backend-network')

383

client.connect_container_to_network(database_container, 'backend-network')

384

385

# Frontend can reach backend, backend can reach database,

386

# but frontend cannot directly reach database

387

```

388

389

### Dynamic Network Management

390

391

```python

392

# Function to create environment-specific networks

393

def create_environment_network(env_name):

394

network_name = f'{env_name}-network'

395

396

# Create network with environment-specific configuration

397

network = client.create_network(

398

name=network_name,

399

driver='bridge',

400

labels={

401

'environment': env_name,

402

'created_by': 'docker-py'

403

},

404

options={

405

'com.docker.network.bridge.name': f'{env_name}-br0'

406

}

407

)

408

409

return network['Id']

410

411

# Create networks for different environments

412

environments = ['development', 'staging', 'production']

413

network_ids = {}

414

415

for env in environments:

416

network_ids[env] = create_environment_network(env)

417

print(f"Created {env} network: {network_ids[env][:12]}")

418

419

# Clean up networks

420

for env in environments:

421

try:

422

client.remove_network(f'{env}-network')

423

print(f"Removed {env} network")

424

except Exception as e:

425

print(f"Error removing {env} network: {e}")

426

```