Network
is a class for working with Docker networks through the API. It provides methods for creating, inspecting, connecting containers, and managing Docker networks.
Sangezar\DockerClient\Api
The Network
class inherits from AbstractApi
and implements the NetworkInterface
interface.
public const DRIVER_BRIDGE = 'bridge';
public const DRIVER_HOST = 'host';
public const DRIVER_OVERLAY = 'overlay';
public const DRIVER_MACVLAN = 'macvlan';
public const DRIVER_IPVLAN = 'ipvlan';
public const DRIVER_NONE = 'none';
public const SCOPE_LOCAL = 'local';
public const SCOPE_SWARM = 'swarm';
public const SCOPE_GLOBAL = 'global';
public const IPAM_DRIVER_DEFAULT = 'default';
public const IPAM_DRIVER_NULL = 'null';
public function list(array $filters = []): array
Gets a list of networks.
$filters
- Filters to apply in JSON format or array:
driver
(string) - Filter by network driverid
(string) - Network IDlabel
(string) - Filter by network labelsname
(string) - Network namescope
(string) - Filter by network scope (swarm, global, or local)type
(string) - Filter by network type (custom or builtin)InvalidParameterValueException
- if invalid filters are passedpublic function inspect(string $id): array
Gets detailed information about a network.
$id
- Network ID or nameMissingRequiredParameterException
- if the network ID is emptyNotFoundException
- if the network is not foundpublic function create(NetworkConfig $config): array
Creates a new network.
$config
- NetworkConfig
object with network configurationInvalidConfigurationException
- if the configuration is invalidOperationFailedException
- if the operation failspublic function connect(string $id, string $container, array $config = []): bool
Connects a container to a network.
$id
- Network ID or name$container
- Container ID or name$config
- Connection configuration:
EndpointConfig
(array) - Endpoint configurationIPAddress
(string) - IPv4 addressIPv6Address
(string) - IPv6 addressLinks
(array) - Links to other containersAliases
(array) - Names to use in the networktrue
if the connection is successfulMissingRequiredParameterException
- if required parameters are missingNotFoundException
- if the network or container is not foundOperationFailedException
- if the connection failspublic function disconnect(string $networkId, string $containerId, array $config = []): bool
Disconnects a container from a network.
$networkId
- Network ID or name$containerId
- Container ID or name$config
- Disconnection configuration:
Force
(bool) - Force disconnection of the container even if it results in a communication errortrue
if the disconnection is successfulMissingRequiredParameterException
- if required parameters are missingNotFoundException
- if the network or container is not foundOperationFailedException
- if the disconnection failspublic function remove(string $id): bool
Removes a network.
$id
- Network ID or nametrue
if the network was successfully removedMissingRequiredParameterException
- if the network ID is emptyNotFoundException
- if the network is not foundOperationFailedException
- if removal fails (e.g., network is in use)public function prune(array $filters = []): array
Removes all unused networks.
$filters
- Filters for determining networks to clean up:
until
(string) - Remove networks created before the specified timelabel
(string) - Remove only networks with specified labelsInvalidParameterValueException
- if filters are invalidOperationFailedException
- if cleanup failspublic function exists(string $id): bool
Checks if a network exists.
$id
- Network ID or nametrue
if the network exists, false
otherwiseMissingRequiredParameterException
- if the network ID is emptyuse Sangezar\DockerClient\DockerClient;
$client = DockerClient::createUnix();
$networks = $client->network()->list();
foreach ($networks as $network) {
echo "Network: {$network['Name']}, driver: {$network['Driver']}\n";
}
use Sangezar\DockerClient\DockerClient;
use Sangezar\DockerClient\Config\NetworkConfig;
$client = DockerClient::createUnix();
$config = new NetworkConfig();
$config->setName('my-network')
->setDriver(Network::DRIVER_BRIDGE)
->setOptions([
'com.docker.network.bridge.name' => 'my-bridge'
]);
$network = $client->network()->create($config);
echo "Network created with ID: {$network['Id']}\n";
use Sangezar\DockerClient\DockerClient;
$client = DockerClient::createUnix();
$networkId = 'my-network';
$containerId = 'my-container';
$client->network()->connect($networkId, $containerId, [
'Aliases' => ['web-server'],
'IPAddress' => '172.18.0.10'
]);
echo "Container {$containerId} connected to network {$networkId}\n";
use Sangezar\DockerClient\DockerClient;
$client = DockerClient::createUnix();
$networkId = 'my-network';
if ($client->network()->exists($networkId)) {
$client->network()->remove($networkId);
echo "Network {$networkId} removed\n";
}