A Docker client for Python, designed to be fun and intuitive!
—
Docker network creation, management, and container connectivity operations. Networks provide isolated communication channels for containers and enable service discovery within Docker environments.
Create custom networks with specific drivers and configuration options.
def create(
name: str,
*,
driver: Optional[str] = None,
options: Optional[Dict[str, str]] = None,
ipam_driver: Optional[str] = None,
ipam_config: Optional[List[Dict[str, str]]] = None,
enable_ipv6: bool = False,
internal: bool = False,
labels: Optional[Dict[str, str]] = None,
scope: Optional[str] = None,
attachable: bool = False,
ingress: bool = False,
config_only: bool = False,
config_from: Optional[str] = None
) -> Network:
"""
Create a new network.
Parameters:
- name: Network name
- driver: Network driver (bridge, overlay, host, none, macvlan)
- options: Driver-specific options
- ipam_driver: IPAM driver name
- ipam_config: IPAM configuration (subnet, gateway, ip_range, aux_addresses)
- enable_ipv6: Enable IPv6 networking
- internal: Restrict external access to network
- labels: Network labels
- scope: Network scope (local, global, swarm)
- attachable: Enable manual container attachment
- ingress: Create swarm routing-mesh network
- config_only: Create configuration-only network
- config_from: Network to copy configuration from
Returns:
- Network object
"""List and inspect networks with filtering and detailed information retrieval.
def list(
filters: Optional[Dict[str, str]] = None,
*,
quiet: bool = False,
no_trunc: bool = False
) -> List[Network]:
"""
List networks.
Parameters:
- filters: Filters to apply (driver, name, id, label, scope, type)
- quiet: Only show network IDs
- no_trunc: Don't truncate output
Returns:
- List of Network objects
"""
def inspect(network: str) -> Network:
"""
Get detailed information about a network.
Parameters:
- network: Network name or ID
Returns:
- Network object with full details
"""Connect and disconnect containers from networks.
def connect(
network: str,
container: str,
*,
aliases: Optional[List[str]] = None,
ip: Optional[str] = None,
ipv6_address: Optional[str] = None,
links: Optional[List[str]] = None,
driver_options: Optional[Dict[str, str]] = None
) -> None:
"""
Connect container to network.
Parameters:
- network: Network name or ID
- container: Container name or ID
- aliases: Network-scoped aliases for container
- ip: IPv4 address for container
- ipv6_address: IPv6 address for container
- links: Container links (deprecated)
- driver_options: Driver-specific options
"""
def disconnect(
network: str,
container: str,
*,
force: bool = False
) -> None:
"""
Disconnect container from network.
Parameters:
- network: Network name or ID
- container: Container name or ID
- force: Force disconnect
"""Remove networks and clean up unused networks.
def remove(networks: Union[str, List[str]]) -> None:
"""
Remove networks.
Parameters:
- networks: Network name(s) or ID(s)
"""
def prune(filters: Optional[Dict[str, str]] = None) -> Dict[str, Any]:
"""
Remove unused networks.
Parameters:
- filters: Filters to apply
Returns:
- Information about removed networks
"""from python_on_whales import docker
# Create a custom bridge network
network = docker.network.create(
"my-app-network",
driver="bridge",
labels={"project": "my-app"}
)
# List all networks
networks = docker.network.list()
for net in networks:
print(f"Network: {net.name} - Driver: {net.driver}")
# Connect container to network
docker.network.connect("my-app-network", "my-container")
# Disconnect and remove
docker.network.disconnect("my-app-network", "my-container")
docker.network.remove("my-app-network")# Create overlay network for swarm
overlay_network = docker.network.create(
"my-overlay",
driver="overlay",
attachable=True,
ipam_config=[{
"subnet": "10.0.0.0/24",
"gateway": "10.0.0.1"
}]
)
# Create macvlan network
macvlan_network = docker.network.create(
"my-macvlan",
driver="macvlan",
options={
"parent": "eth0"
},
ipam_config=[{
"subnet": "192.168.1.0/24",
"gateway": "192.168.1.1",
"ip_range": "192.168.1.128/25"
}]
)class Network:
id: str
name: str
driver: str
scope: str
created: str
internal: bool
attachable: bool
ingress: bool
ipam: Dict[str, Any]
containers: Dict[str, Dict[str, Any]]
options: Dict[str, str]
labels: Dict[str, str]
def remove(self) -> None: ...
def connect(self, container: str, **kwargs) -> None: ...
def disconnect(self, container: str, force: bool = False) -> None: ...
def reload(self) -> None: ...Install with Tessl CLI
npx tessl i tessl/pypi-python-on-whales