Networks

Create and manage networks on the server. For more information about networks, see the Engine documentation.

Methods available on client.networks:

class NetworkCollection
create(name, *args, **kwargs)

Create a network. Similar to the docker network create.

Parameters:
  • name (str) – Name of the network
  • driver (str) – Name of the driver used to create the network
  • options (dict) – Driver options as a key-value dictionary
  • ipam (IPAMConfig) – Optional custom IP scheme for the network.
  • check_duplicate (bool) – Request daemon to check for networks with same name. Default: None.
  • internal (bool) – Restrict external access to the network. Default False.
  • labels (dict) – Map of labels to set on the network. Default None.
  • enable_ipv6 (bool) – Enable IPv6 on the network. Default False.
  • attachable (bool) – If enabled, and the network is in the global scope, non-service containers on worker nodes will be able to connect to the network.
  • scope (str) – Specify the network’s scope (local, global or swarm)
  • ingress (bool) – If set, create an ingress network which provides the routing-mesh in swarm mode.
Returns:

The network that was created.

Return type:

(Network)

Raises:

docker.errors.APIError – If the server returns an error.

Example

A network using the bridge driver:

>>> client.networks.create("network1", driver="bridge")

You can also create more advanced networks with custom IPAM configurations. For example, setting the subnet to 192.168.52.0/24 and gateway address to 192.168.52.254.

>>> ipam_pool = docker.types.IPAMPool(
    subnet='192.168.52.0/24',
    gateway='192.168.52.254'
)
>>> ipam_config = docker.types.IPAMConfig(
    pool_configs=[ipam_pool]
)
>>> client.networks.create(
    "network1",
    driver="bridge",
    ipam=ipam_config
)
get(network_id, *args, **kwargs)

Get a network by its ID.

Parameters:
  • network_id (str) – The ID of the network.
  • verbose (bool) – Retrieve the service details across the cluster in swarm mode.
  • scope (str) – Filter the network by scope (swarm, global or local).
Returns:

(Network) The network.

Raises:
  • docker.errors.NotFound – If the network does not exist.
  • docker.errors.APIError – If the server returns an error.
list(*args, **kwargs)

List networks. Similar to the docker networks ls command.

Parameters:
  • names (list) – List of names to filter by.
  • ids (list) – List of ids to filter by.
  • filters (dict) –

    Filters to be processed on the network list. Available filters: - driver=[<driver-name>] Matches a network’s driver. - label (str|list): format either "key", "key=value"

    or a list of such.
    • type=["custom"|"builtin"] Filters networks by type.
  • greedy (bool) – Fetch more details for each network individually. You might want this to get the containers attached to them.
Returns:

(list of Network) The networks on the server.

Raises:

docker.errors.APIError – If the server returns an error.

prune(filters=None)

Delete unused networks

Parameters:filters (dict) – Filters to process on the prune list.
Returns:
A dict containing a list of deleted network names and
the amount of disk space reclaimed in bytes.
Return type:(dict)
Raises:docker.errors.APIError – If the server returns an error.

Network objects

class Network

A Docker network.

id

The ID of the object.

short_id

The ID of the object, truncated to 10 characters.

name

The name of the network.

containers

The containers that are connected to the network, as a list of Container objects.

attrs

The raw representation of this object from the server.

connect(container, *args, **kwargs)

Connect a container to this network.

Parameters:
  • container (str) – Container to connect to this network, as either an ID, name, or Container object.
  • aliases (list) – A list of aliases for this endpoint. Names in that list can be used within the network to reach the container. Defaults to None.
  • links (list) – A list of links for this endpoint. Containers declared in this list will be linkedto this container. Defaults to None.
  • ipv4_address (str) – The IP address of this container on the network, using the IPv4 protocol. Defaults to None.
  • ipv6_address (str) – The IP address of this container on the network, using the IPv6 protocol. Defaults to None.
  • link_local_ips (list) – A list of link-local (IPv4/IPv6) addresses.
  • driver_opt (dict) – A dictionary of options to provide to the network driver. Defaults to None.
Raises:

docker.errors.APIError – If the server returns an error.

disconnect(container, *args, **kwargs)

Disconnect a container from this network.

Parameters:
  • container (str) – Container to disconnect from this network, as either an ID, name, or Container object.
  • force (bool) – Force the container to disconnect from a network. Default: False
Raises:

docker.errors.APIError – If the server returns an error.

reload()

Load this object from the server again and update attrs with the new data.

remove()

Remove this network.

Raises:docker.errors.APIError – If the server returns an error.