The NetworkConfig class is part of the Sangezar\DockerClient\Config namespace and provides a convenient interface for configuring Docker networks.
namespace Sangezar\DockerClient\Config;
The NetworkConfig class allows you to create and configure Docker network configurations that can be used by the API client to create new networks.
$networkConfig = NetworkConfig::create();
Static method for creating a new network configuration instance.
$networkConfig = NetworkConfig::create();
Sets the network name.
Parameters:
$name - Network nameExceptions:
InvalidParameterValueException - if the network name is empty or invalid.$networkConfig->setName('my-network');
Sets the network driver.
Parameters:
$driver - Network driver. Valid values:
bridge - standard bridgehost - uses the host’s network stackoverlay - overlay networkmacvlan - MAC VLAN networkipvlan - IP VLAN networknone - no networkExceptions:
InvalidParameterValueException - if the driver is unknown.$networkConfig->setDriver('bridge');
Enables or disables IPv6 support.
Parameters:
$enable - true to enable IPv6, false to disable.$networkConfig->setEnableIPv6(true);
Sets whether the network should be internal.
Parameters:
$internal - true for internal network, false for external.$networkConfig->setInternal(true);
Sets whether containers can be attached to the network.
Parameters:
$attachable - true if containers can join, otherwise false.$networkConfig->setAttachable(true);
Sets the network scope.
Parameters:
$scope - Network scope. Valid values:
local - local networkswarm - swarm networkglobal - global networkExceptions:
InvalidParameterValueException - if the scope is unknown.$networkConfig->setScope('local');
Adds a subnet to the IPAM configuration.
Parameters:
$subnet - Subnet in CIDR format (e.g., 192.168.0.0/24)$gateway - Gateway IP address (optional)$ipRange - IP range in CIDR format (optional)Exceptions:
InvalidParameterValueException - if parameters are invalid.$networkConfig->addSubnet('192.168.1.0/24', '192.168.1.1');
Sets the IPAM driver.
Parameters:
$driver - IPAM driver. Valid values:
default - standard drivernull - null driverExceptions:
InvalidParameterValueException - if the driver is unknown.$networkConfig->setIpamDriver('default');
Adds a driver option.
Parameters:
$key - Option key$value - Option value$networkConfig->addOption('com.docker.network.bridge.name', 'my-bridge');
Adds a label to the network.
Parameters:
$key - Label key$value - Label value$networkConfig->addLabel('com.example.description', 'Network for web applications');
Converts the configuration to an array for the Docker API.
Exceptions:
InvalidConfigurationException - if the configuration is invalid.Returns:
array<string, mixed> - Configuration array for the Docker API.$configArray = $networkConfig->toArray();
use Sangezar\DockerClient\Config\NetworkConfig;
// Creating a network configuration
$networkConfig = NetworkConfig::create()
->setName('my-web-network')
->setDriver('bridge')
->setEnableIPv6(true)
->addSubnet('192.168.0.0/24', '192.168.0.1')
->addLabel('environment', 'production')
->addOption('com.docker.network.bridge.enable_icc', 'true');
// Converting to an array for the Docker API
$configArray = $networkConfig->toArray();
// Creating a network using the API client
$dockerClient->networks()->create($networkConfig);