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.
Sangezar\DockerClient\Cluster
// 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_.-]*$/';
public function addNode(string $name, DockerClient $client, array $tags = []): self
Adds a new node to the cluster.
$name
- Unique node name$client
- Docker API client$tags
- Array of tags for categorizing the node (default empty array)DockerCluster
instance (for chained calls)ValidationException
- if the node name is invalid or already existspublic function node(string $name): DockerClient
Returns a node client by its name.
$name
- Node nameDockerClient
instance for the specified nodeNodeNotFoundException
- if the node is not foundValidationException
- if the node name is emptypublic function hasNode(string $name): bool
Checks if a node with the specified name exists.
$name
- Node nametrue
if the node exists, false
otherwisepublic function removeNode(string $name): self
Removes a node from the cluster.
$name
- Node nameDockerCluster
instance (for chained calls)ValidationException
- if the node name is emptypublic function getNodesByTag(string $tag): array
Returns all nodes with the specified tag.
$tag
- Tag to filter byValidationException
- if the tag is invalidpublic function addTagToNode(string $nodeName, string $tag): self
Adds a tag to an existing node.
$nodeName
- Node name$tag
- Tag to addDockerCluster
instance (for chained calls)NodeNotFoundException
- if the node is not foundValidationException
- if the tag is invalid or the node name is emptypublic function removeTagFromNode(string $nodeName, string $tag): self
Removes a tag from a node.
$nodeName
- Node name$tag
- Tag to removeDockerCluster
instance (for chained calls)NodeNotFoundException
- if the node is not foundValidationException
- if the node name is emptypublic function getNodesByAllTags(array $tags): array
Returns all nodes that have all the specified tags (AND operation).
$tags
- Array of tagsValidationException
- if any tag is invalidpublic function getNodesByAnyTag(array $tags): array
Returns all nodes that have at least one of the specified tags (OR operation).
$tags
- Array of tagsValidationException
- if any tag is invalidpublic function filter(callable $callback): NodeCollection
Returns a collection of nodes filtered using a callback function.
$callback
- Callback function for filtering that takes a node as an argument and returns a boolean valueNodeCollection
instance with filtered nodespublic function all(): NodeCollection
Returns a collection of all cluster nodes.
NodeCollection
instance with all nodespublic function byTag(string $tag): NodeCollection
Returns a collection of nodes with the specified tag.
$tag
- Tag to filter byNodeCollection
instance with nodes that have the specified tagpublic function byAllTags(array $tags): NodeCollection
Returns a collection of nodes that have all the specified tags.
$tags
- Array of tagsNodeCollection
instance with nodes that have all the specified tagspublic function byAnyTag(array $tags): NodeCollection
Returns a collection of nodes that have at least one of the specified tags.
$tags
- Array of tagsNodeCollection
instance with nodes that have at least one of the specified tagspublic function addNodes(array $nodes): self
Adds multiple nodes to the cluster.
$nodes
- Array of nodes to add, where keys:
name
(string) - Node nameclient
(DockerClient) - Docker API clienttags
(array, optional) - Array of tagsDockerCluster
instance (for chained calls)addNode
methodpublic function getNodes(): array
Returns all cluster nodes.
DockerClient
instancespublic function getTags(): array
Returns all cluster tags.
public function isEmpty(): bool
Checks if the cluster is empty.
true
if the cluster has no nodes, false
otherwisepublic function count(): int
Counts the number of nodes in the cluster.
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 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";
// 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";
}
}
// 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";
}