docker-php-client

DockerCluster Class Documentation

Description

DockerCluster is a class that represents a cluster of Docker servers. It allows you to manage a set of Docker nodes, group them using tags, and perform operations on individual nodes as well as on groups of nodes.

Namespace

Sangezar\DockerClient\Cluster

Constants

// Regular expression for node name validation
private const NODE_NAME_PATTERN = '/^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/';

// Regular expression for tag name validation
private const TAG_NAME_PATTERN = '/^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/';

Methods

addNode

public function addNode(string $name, DockerClient $client, array $tags = []): self

Adds a new node to the cluster.

Parameters:

Returns:

Exceptions:

node

public function node(string $name): DockerClient

Returns a node client by its name.

Parameters:

Returns:

Exceptions:

hasNode

public function hasNode(string $name): bool

Checks if a node with the specified name exists.

Parameters:

Returns:

removeNode

public function removeNode(string $name): self

Removes a node from the cluster.

Parameters:

Returns:

Exceptions:

getNodesByTag

public function getNodesByTag(string $tag): array

Returns all nodes with the specified tag.

Parameters:

Returns:

Exceptions:

addTagToNode

public function addTagToNode(string $nodeName, string $tag): self

Adds a tag to an existing node.

Parameters:

Returns:

Exceptions:

removeTagFromNode

public function removeTagFromNode(string $nodeName, string $tag): self

Removes a tag from a node.

Parameters:

Returns:

Exceptions:

getNodesByAllTags

public function getNodesByAllTags(array $tags): array

Returns all nodes that have all the specified tags (AND operation).

Parameters:

Returns:

Exceptions:

getNodesByAnyTag

public function getNodesByAnyTag(array $tags): array

Returns all nodes that have at least one of the specified tags (OR operation).

Parameters:

Returns:

Exceptions:

filter

public function filter(callable $callback): NodeCollection

Returns a collection of nodes filtered using a callback function.

Parameters:

Returns:

all

public function all(): NodeCollection

Returns a collection of all cluster nodes.

Returns:

byTag

public function byTag(string $tag): NodeCollection

Returns a collection of nodes with the specified tag.

Parameters:

Returns:

byAllTags

public function byAllTags(array $tags): NodeCollection

Returns a collection of nodes that have all the specified tags.

Parameters:

Returns:

byAnyTag

public function byAnyTag(array $tags): NodeCollection

Returns a collection of nodes that have at least one of the specified tags.

Parameters:

Returns:

addNodes

public function addNodes(array $nodes): self

Adds multiple nodes to the cluster.

Parameters:

Returns:

Exceptions:

getNodes

public function getNodes(): array

Returns all cluster nodes.

Returns:

getTags

public function getTags(): array

Returns all cluster tags.

Returns:

isEmpty

public function isEmpty(): bool

Checks if the cluster is empty.

Returns:

count

public function count(): int

Counts the number of nodes in the cluster.

Returns:

Usage Examples

Creating a Cluster and Adding Nodes

use Sangezar\DockerClient\DockerClient;
use Sangezar\DockerClient\Cluster\DockerCluster;

// Creating a cluster
$cluster = new DockerCluster();

// Adding nodes with tags
$cluster->addNode(
    'node1',
    DockerClient::createTcp('tcp://192.168.1.10:2375'),
    ['production', 'web']
);

$cluster->addNode(
    'node2',
    DockerClient::createTcp('tcp://192.168.1.11:2375'),
    ['production', 'database']
);

$cluster->addNode(
    'node3',
    DockerClient::createTcp('tcp://192.168.1.12:2375'),
    ['staging', 'web']
);

// Checking the number of nodes
echo "Total number of nodes: " . $cluster->count() . "\n";

Getting Nodes by Tags

// Getting all nodes with the 'production' tag
$productionNodes = $cluster->getNodesByTag('production');
echo "Nodes with the 'production' tag: " . implode(', ', array_keys($productionNodes)) . "\n";

// Getting nodes that have both 'production' and 'web' tags
$productionWebNodes = $cluster->getNodesByAllTags(['production', 'web']);
echo "Nodes with 'production' and 'web' tags: " . implode(', ', array_keys($productionWebNodes)) . "\n";

// Getting nodes that have either 'production' or 'staging' tag
$allEnvNodes = $cluster->getNodesByAnyTag(['production', 'staging']);
echo "Nodes with 'production' or 'staging' tags: " . implode(', ', array_keys($allEnvNodes)) . "\n";

Performing Operations on a Group of Nodes

// Getting a collection of nodes by tag and performing operations
$webNodesCollection = $cluster->byTag('web');

// Getting a list of all containers on web nodes
$containersMap = $webNodesCollection->containers()->list(['all' => true]);

foreach ($containersMap as $nodeName => $containers) {
    echo "Node: $nodeName\n";
    foreach ($containers as $container) {
        echo "  Container: {$container['Names'][0]}\n";
    }
}

Managing Tags

// Adding a tag to a node
$cluster->addTagToNode('node3', 'monitoring');

// Removing a tag from a node
$cluster->removeTagFromNode('node2', 'production');

// Checking if a node exists
if ($cluster->hasNode('node1')) {
    echo "Node 'node1' exists\n";
    
    // Performing operations on an individual node
    $node1Client = $cluster->node('node1');
    $containers = $node1Client->container()->list(['all' => true]);
    echo "Number of containers on node 'node1': " . count($containers) . "\n";
}