ImageOperations
is a class that provides operations with Docker images on all cluster nodes simultaneously. The class allows you to get a list of images, build, create (pull), inspect, push to registry, tag, and delete Docker images on all cluster nodes.
Sangezar\DockerClient\Cluster\Operations
The class extends AbstractOperations
and inherits all its methods and properties.
public function list(array $parameters = []): array
Gets a list of images from all cluster nodes.
$parameters
- Array of filtering parameters:
all
(bool): Show all images (default false)filters
(array): Filters for searching imagesInvalidParameterValueException
- if invalid filtering parameters are providedpublic function build(array $parameters = [], array $config = []): array
Builds an image on all cluster nodes.
$parameters
- Build parameters:
t
or tag
(string): Image tag (required)$config
- Additional configuration:
context
(string): Path to the build contextInvalidParameterValueException
- if invalid parameters are providedInvalidConfigurationException
- if the configuration is invalidMissingRequiredParameterException
- if the required tag parameter is missingpublic function buildWithOptions(\Sangezar\DockerClient\Config\ImageBuildOptions $options): array
Builds an image on all cluster nodes using a build options object.
$options
- Image build options objectInvalidParameterValueException
- if invalid parameters are providedInvalidConfigurationException
- if the configuration is invalidpublic function create(string $fromImage, ?string $tag = null): array
Creates an image by pulling it from the registry, on all cluster nodes.
$fromImage
- Name of the image to pull$tag
- Image tag (default “latest”)MissingRequiredParameterException
- if the image name is emptyInvalidParameterValueException
- if invalid parameters are providedpublic function inspect(string $name): array
Gets detailed information about an image on all cluster nodes.
$name
- Image name or IDMissingRequiredParameterException
- if the image name is emptypublic function history(string $name): array
Gets the image history on all cluster nodes.
$name
- Image name or IDMissingRequiredParameterException
- if the image name is emptypublic function push(string $name, array $parameters = []): array
Pushes an image to the registry from all cluster nodes.
$name
- Name of the image to push$parameters
- Additional parametersMissingRequiredParameterException
- if the image name is emptypublic function tag(string $name, string $repo, ?string $tag = null): array
Tags an image on all cluster nodes.
$name
- Name or ID of the source image$repo
- Repository for the new tag$tag
- New tag (if null, “latest” is used)MissingRequiredParameterException
- if the image name or repository is emptyInvalidParameterValueException
- if invalid parameters are providedpublic function remove(string $name, bool $force = false, bool $noprune = false): array
Removes an image on all cluster nodes.
$name
- Image name or ID$force
- Force image removal (default false)$noprune
- Do not delete intermediate images (default false)MissingRequiredParameterException
- if the image name is emptypublic function search(string $term): array
Searches for images in Docker Hub from all cluster nodes.
$term
- Search termMissingRequiredParameterException
- if the search term is emptypublic function prune(array $parameters = []): array
Removes unused images on all cluster nodes.
$parameters
- Parameters for removal:
filters
(array): Filters for selecting images to removepublic function exists(string $name): array
Checks if an image exists on all cluster nodes.
$name
- Image name or IDMissingRequiredParameterException
- if the image name is emptypublic function existsOnAllNodes(string $name): bool
Checks if an image exists on all cluster nodes.
$name
- Image name or IDtrue
if the image exists on all nodes, false
if it’s missing on at least one nodeMissingRequiredParameterException
- if the image name is emptypublic function getNodesWithImage(string $name): array
Gets a list of nodes where the image exists.
$name
- Image name or IDMissingRequiredParameterException
- if the image name is emptypublic function pull(string $name, array $parameters = []): array
Pulls an image from the registry to all cluster nodes.
$name
- Name of the image to pull$parameters
- Additional parameters for pullingMissingRequiredParameterException
- if the image name is emptyInvalidParameterValueException
- if invalid parameters are providedpublic function load(string $imageArchive): array
Loads an image from an archive to all cluster nodes.
$imageArchive
- Path to the image archiveMissingRequiredParameterException
- if the archive path is emptyFileNotFoundException
- if the archive file is not foundpublic function save($names, string $outputFile): array
Saves images to an archive from all cluster nodes.
$names
- Name or array of image names to save$outputFile
- Path to the output fileMissingRequiredParameterException
- if the image name or output path is emptyInvalidParameterValueException
- if invalid parameters are provideduse Sangezar\DockerClient\Cluster\DockerCluster;
use Sangezar\DockerClient\DockerClient;
// Creating a cluster
$cluster = new DockerCluster();
$cluster->addNode('node1', DockerClient::createTcp('tcp://192.168.1.10:2375'));
$cluster->addNode('node2', DockerClient::createTcp('tcp://192.168.1.11:2375'));
// Getting a list of all images
$images = $cluster->images()->list(['all' => true]);
// Checking results
foreach ($images as $nodeName => $result) {
echo "Images on node $nodeName:\n";
foreach ($result as $image) {
echo " - {$image['RepoTags'][0]} (ID: {$image['Id']})\n";
}
}
use Sangezar\DockerClient\Cluster\NodeCollection;
use Sangezar\DockerClient\DockerClient;
// Creating a node collection
$nodes = [
'node1' => DockerClient::createTcp('tcp://192.168.1.10:2375'),
'node2' => DockerClient::createTcp('tcp://192.168.1.11:2375'),
];
$collection = new NodeCollection($nodes);
// Pulling an image to all nodes
$results = $collection->images()->pull('nginx:latest');
// Checking if the image exists on all nodes
$exists = $collection->images()->existsOnAllNodes('nginx:latest');
if ($exists) {
echo "Image 'nginx:latest' exists on all nodes\n";
} else {
// Getting a list of nodes where the image exists
$nodesWithImage = $collection->images()->getNodesWithImage('nginx:latest');
echo "Image 'nginx:latest' exists only on nodes: " . implode(', ', $nodesWithImage) . "\n";
}
// Building an image on all nodes
$buildResults = $collection->images()->build([
't' => 'myapp:latest',
'dockerfile' => 'Dockerfile',
], [
'context' => '/path/to/build/context',
]);
// Tagging the image for pushing
$collection->images()->tag('myapp:latest', 'registry.example.com/myapp', 'latest');
// Pushing the image to the registry
$pushResults = $collection->images()->push('registry.example.com/myapp:latest');
// Removing the local image
$collection->images()->remove('myapp:latest', true);