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.
Sangezar\DockerClient\Api
The Image
class inherits from AbstractApi
and implements the ImageInterface
interface.
public function list(array $parameters = []): array
Gets a list of images.
$parameters
- Array of parameters for filtering results:
all
(bool) - Show all images (by default hides intermediate images)filters (array |
string) - Filters to apply in JSON format or array |
digests
(bool) - Show digest informationInvalidParameterValueException
- if invalid parameters are passedpublic function build(array $parameters = [], array $config = []): array
Builds a new image.
$parameters
- Request parameters for building:
t
(string) - Name and tag for the built imageq
(bool) - Suppress detailed build outputnocache
(bool) - Do not use cache when building the imagepull (bool |
string) - Download the image before building |
rm
(bool) - Remove intermediate containers after successful buildforcerm
(bool) - Always remove intermediate containers$config
- Configuration for the build contextInvalidParameterValueException
- if invalid parameters are passedOperationFailedException
- if the build failspublic function buildWithOptions(\Sangezar\DockerClient\Config\ImageBuildOptions $options): array
Builds an image using an ImageBuildOptions object.
$options
- Configuration options for building the imageInvalidParameterValueException
- if invalid parameters are passedOperationFailedException
- if the build failspublic function create(string $fromImage, ?string $tag = null): array
Creates an image by downloading it from the registry.
$fromImage
- Source image name$tag
- Tag to download (default ‘latest’)MissingRequiredParameterException
- if fromImage is emptyInvalidParameterValueException
- if the image name format or tag is invalidOperationFailedException
- if the download failspublic function inspect(string $name): array
Gets detailed information about an image.
$name
- Image name or IDMissingRequiredParameterException
- if the image name is emptyNotFoundException
- if the image is not foundpublic function history(string $name): array
Gets the image history.
$name
- Image name or IDMissingRequiredParameterException
- if the image name is emptyNotFoundException
- if the image is not foundpublic function push(string $name, array $parameters = []): array
Pushes an image to the registry.
$name
- Image name$parameters
- Additional parameters:
tag
(string) - Tag to pushMissingRequiredParameterException
- if the image name is emptyOperationFailedException
- if the push failspublic function tag(string $name, string $repo, ?string $tag = null): bool
Tags an image with a new name and tag.
$name
- Image name or ID$repo
- New repository name for the image$tag
- New tag (default ‘latest’)true
if the image was successfully taggedMissingRequiredParameterException
- if required parameters are emptyInvalidParameterValueException
- if the repository format or tag is invalidOperationFailedException
- if tagging failsNotFoundException
- if the image is not foundpublic function exists(string $name): bool
Checks if an image exists.
$name
- Image name or IDtrue
if the image exists, false
otherwiseMissingRequiredParameterException
- if the image name is emptypublic function remove(string $name, bool $force = false, bool $noprune = false): bool
Removes an image.
$name
- Image name or ID$force
- Force removal (default false
)$noprune
- Do not delete unused parent layers (default false
)true
if the image was successfully removedMissingRequiredParameterException
- if the image name is emptyOperationFailedException
- if removal failsNotFoundException
- if the image is not foundpublic function search(string $term): array
Searches for images in the Docker Hub registry.
$term
- Search queryMissingRequiredParameterException
- if the search query is emptyOperationFailedException
- if the search failspublic function prune(array $filters = []): array
Removes unused images.
$filters
- Filters for selecting images to clean up:
dangling
(bool) - Remove only dangling images (images without tags)until
(string) - Remove images created before the specified timelabel
(string) - Remove images with specified labelsInvalidParameterValueException
- if filters are invalidOperationFailedException
- if cleanup failsuse 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";
}
use Sangezar\DockerClient\DockerClient;
$client = DockerClient::createUnix();
$result = $client->image()->create('nginx', 'latest');
echo "Image nginx:latest successfully downloaded\n";
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";
}