docker-php-client

Image Class Documentation

Description

Image is a class for working with Docker images through the API. It provides methods for creating, downloading, getting information, tagging, and removing Docker images.

Namespace

Sangezar\DockerClient\Api

Inheritance

The Image class inherits from AbstractApi and implements the ImageInterface interface.

Methods

list

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

Gets a list of images.

Parameters:

Returns:

Exceptions:

build

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

Builds a new image.

Parameters:

Returns:

Exceptions:

buildWithOptions

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

Builds an image using an ImageBuildOptions object.

Parameters:

Returns:

Exceptions:

create

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

Creates an image by downloading it from the registry.

Parameters:

Returns:

Exceptions:

inspect

public function inspect(string $name): array

Gets detailed information about an image.

Parameters:

Returns:

Exceptions:

history

public function history(string $name): array

Gets the image history.

Parameters:

Returns:

Exceptions:

push

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

Pushes an image to the registry.

Parameters:

Returns:

Exceptions:

tag

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

Tags an image with a new name and tag.

Parameters:

Returns:

Exceptions:

exists

public function exists(string $name): bool

Checks if an image exists.

Parameters:

Returns:

Exceptions:

remove

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

Removes an image.

Parameters:

Returns:

Exceptions:

public function search(string $term): array

Searches for images in the Docker Hub registry.

Parameters:

Returns:

Exceptions:

prune

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

Removes unused images.

Parameters:

Returns:

Exceptions:

Usage Examples

Getting a List of Images

use Sangezar\DockerClient\DockerClient;

$client = DockerClient::createUnix();
$images = $client->image()->list(['all' => true]);

foreach ($images as $image) {
    $tags = $image['RepoTags'] ?? ['<none>:<none>'];
    echo "Image: " . implode(', ', $tags) . ", size: " . $image['Size'] . " bytes\n";
}

Downloading an Image from Docker Hub

use Sangezar\DockerClient\DockerClient;

$client = DockerClient::createUnix();
$result = $client->image()->create('nginx', 'latest');
echo "Image nginx:latest successfully downloaded\n";

Tagging and Removing an Image

use Sangezar\DockerClient\DockerClient;

$client = DockerClient::createUnix();
$imageName = 'nginx:latest';

if ($client->image()->exists($imageName)) {
    // Tag the image with a new name
    $client->image()->tag($imageName, 'my-nginx', 'v1');
    
    // Remove the original image
    $client->image()->remove($imageName);
    
    echo "Image {$imageName} renamed to my-nginx:v1 and original removed\n";
}