NodeCollection
is a class that represents a collection of Docker nodes for performing operations in a cluster. It allows you to perform the same operations on multiple Docker nodes simultaneously.
Sangezar\DockerClient\Cluster
public function __construct(array $nodes)
Creates a new node collection.
$nodes
- Associative array of nodes where keys are node names and values are DockerClient
instancespublic function containers(): ContainerOperations
Gets an object for working with containers on all nodes in the collection.
ContainerOperations
class for working with containerspublic function images(): ImageOperations
Gets an object for working with images on all nodes in the collection.
ImageOperations
class for working with imagespublic function networks(): NetworkOperations
Gets an object for working with networks on all nodes in the collection.
NetworkOperations
class for working with networkspublic function volumes(): VolumeOperations
Gets an object for working with volumes on all nodes in the collection.
VolumeOperations
class for working with volumespublic function system(): SystemOperations
Gets an object for working with system functions on all nodes in the collection.
SystemOperations
class for working with system functionspublic function filter(callable $callback): self
Filters nodes in the collection using a callback function.
$callback
- Callback function for filtering that takes a node as an argument and returns a boolean valueNodeCollection
with filtered nodespublic function getNodes(): array
Gets all nodes in the collection.
DockerClient
instancespublic function count(): int
Counts the number of nodes in the collection.
public function isEmpty(): bool
Checks if the collection is empty.
true
if the collection contains no nodes, false
otherwiseuse Sangezar\DockerClient\DockerClient;
use Sangezar\DockerClient\Cluster\NodeCollection;
// Creating Docker clients for different nodes
$node1 = DockerClient::createTcp('tcp://192.168.1.10:2375');
$node2 = DockerClient::createTcp('tcp://192.168.1.11:2375');
// Creating a node collection
$nodes = [
'node1' => $node1,
'node2' => $node2,
];
$collection = new NodeCollection($nodes);
// Getting a list of containers on all nodes
$containersMap = $collection->containers()->list(['all' => true]);
// Result - an array where keys are node names and values are arrays of containers
foreach ($containersMap as $nodeName => $containers) {
echo "Node: $nodeName\n";
foreach ($containers as $container) {
echo " Container: {$container['Names'][0]}\n";
}
}
use Sangezar\DockerClient\DockerClient;
use Sangezar\DockerClient\Cluster\NodeCollection;
// Creating a node collection
$nodes = [
'node1' => DockerClient::createTcp('tcp://192.168.1.10:2375'),
'node2' => DockerClient::createTcp('tcp://192.168.1.11:2375'),
'node3' => DockerClient::createTcp('tcp://192.168.1.12:2375'),
];
$collection = new NodeCollection($nodes);
// Filtering nodes by name
$filteredCollection = $collection->filter(function ($client, $name) {
return strpos($name, 'node1') === 0 || strpos($name, 'node2') === 0;
});
// Checking the number of nodes after filtering
echo "Number of nodes after filtering: " . $filteredCollection->count() . "\n";
// Performing operations only on filtered nodes
$imagesMap = $filteredCollection->images()->list();