docker-php-client

ImageOperations Class Documentation

Description

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.

Namespace

Sangezar\DockerClient\Cluster\Operations

Inheritance

The class extends AbstractOperations and inherits all its methods and properties.

Methods

list

public function list(array $parameters = []): array

Gets a list of images from all cluster nodes.

Parameters:

Returns:

Exceptions:

build

public function build(array $parameters = [], array $config = []): array

Builds an image on all cluster nodes.

Parameters:

Returns:

Exceptions:

buildWithOptions

public function buildWithOptions(\Sangezar\DockerClient\Config\ImageBuildOptions $options): array

Builds an image on all cluster nodes using a build options object.

Parameters:

Returns:

Exceptions:

create

public function create(string $fromImage, ?string $tag = null): array

Creates an image by pulling it from the registry, on all cluster nodes.

Parameters:

Returns:

Exceptions:

inspect

public function inspect(string $name): array

Gets detailed information about an image on all cluster nodes.

Parameters:

Returns:

Exceptions:

history

public function history(string $name): array

Gets the image history on all cluster nodes.

Parameters:

Returns:

Exceptions:

push

public function push(string $name, array $parameters = []): array

Pushes an image to the registry from all cluster nodes.

Parameters:

Returns:

Exceptions:

tag

public function tag(string $name, string $repo, ?string $tag = null): array

Tags an image on all cluster nodes.

Parameters:

Returns:

Exceptions:

remove

public function remove(string $name, bool $force = false, bool $noprune = false): array

Removes an image on all cluster nodes.

Parameters:

Returns:

Exceptions:

public function search(string $term): array

Searches for images in Docker Hub from all cluster nodes.

Parameters:

Returns:

Exceptions:

prune

public function prune(array $parameters = []): array

Removes unused images on all cluster nodes.

Parameters:

Returns:

exists

public function exists(string $name): array

Checks if an image exists on all cluster nodes.

Parameters:

Returns:

Exceptions:

existsOnAllNodes

public function existsOnAllNodes(string $name): bool

Checks if an image exists on all cluster nodes.

Parameters:

Returns:

Exceptions:

getNodesWithImage

public function getNodesWithImage(string $name): array

Gets a list of nodes where the image exists.

Parameters:

Returns:

Exceptions:

pull

public function pull(string $name, array $parameters = []): array

Pulls an image from the registry to all cluster nodes.

Parameters:

Returns:

Exceptions:

load

public function load(string $imageArchive): array

Loads an image from an archive to all cluster nodes.

Parameters:

Returns:

Exceptions:

save

public function save($names, string $outputFile): array

Saves images to an archive from all cluster nodes.

Parameters:

Returns:

Exceptions:

Usage Examples

Getting a List of Images from All Nodes

use 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";
    }
}

Pulling an Image to All Cluster Nodes

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 and Pushing an Image

// 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);