docker-php-client

NetworkConfig

The NetworkConfig class is part of the Sangezar\DockerClient\Config namespace and provides a convenient interface for configuring Docker networks.

Namespace

namespace Sangezar\DockerClient\Config;

Description

The NetworkConfig class allows you to create and configure Docker network configurations that can be used by the API client to create new networks.

Creating an Instance

$networkConfig = NetworkConfig::create();

Methods

create(): self

Static method for creating a new network configuration instance.

$networkConfig = NetworkConfig::create();

setName(string $name): self

Sets the network name.

Parameters:

Exceptions:

$networkConfig->setName('my-network');

setDriver(string $driver): self

Sets the network driver.

Parameters:

Exceptions:

$networkConfig->setDriver('bridge');

setEnableIPv6(bool $enable = true): self

Enables or disables IPv6 support.

Parameters:

$networkConfig->setEnableIPv6(true);

setInternal(bool $internal = true): self

Sets whether the network should be internal.

Parameters:

$networkConfig->setInternal(true);

setAttachable(bool $attachable = true): self

Sets whether containers can be attached to the network.

Parameters:

$networkConfig->setAttachable(true);

setScope(string $scope): self

Sets the network scope.

Parameters:

Exceptions:

$networkConfig->setScope('local');

addSubnet(string $subnet, ?string $gateway = null, ?string $ipRange = null): self

Adds a subnet to the IPAM configuration.

Parameters:

Exceptions:

$networkConfig->addSubnet('192.168.1.0/24', '192.168.1.1');

setIpamDriver(string $driver): self

Sets the IPAM driver.

Parameters:

Exceptions:

$networkConfig->setIpamDriver('default');

addOption(string $key, string $value): self

Adds a driver option.

Parameters:

$networkConfig->addOption('com.docker.network.bridge.name', 'my-bridge');

addLabel(string $key, string $value): self

Adds a label to the network.

Parameters:

$networkConfig->addLabel('com.example.description', 'Network for web applications');

toArray(): array

Converts the configuration to an array for the Docker API.

Exceptions:

Returns:

$configArray = $networkConfig->toArray();

Usage Example

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);