Volume
is a class for working with Docker volumes through API. It provides methods for creating, inspecting, deleting, and managing Docker volumes.
Sangezar\DockerClient\Api
The Volume
class inherits from AbstractApi
and implements the VolumeInterface
interface.
public const DRIVER_LOCAL = 'local';
public const DRIVER_NFS = 'nfs';
public const DRIVER_TMPFS = 'tmpfs';
public const DRIVER_BTRFS = 'btrfs';
public const DRIVER_VIEUX_BRIDGE = 'vieux-bridge';
public const DRIVER_VFS = 'vfs';
public const DRIVER_CIFS = 'cifs';
public const OPT_TYPE = 'type';
public const OPT_DEVICE = 'device';
public const OPT_O = 'o';
public const OPT_SIZE = 'size';
public function list(array $filters = []): array
Gets a list of volumes.
$filters
- Filters to apply in JSON format or array:
driver
(string) - Filter by volume driverlabel
(string) - Filter by volume labelname
(string) - Filter by volume namedangling
(bool) - Filter volumes not used by any containerInvalidParameterValueException
- if invalid filters are passedpublic function create(VolumeConfig $config): array
Creates a new volume.
$config
- VolumeConfig
object with volume configurationOperationFailedException
- if creation failedpublic function inspect(string $name): array
Gets detailed information about a volume.
$name
- Volume nameMissingRequiredParameterException
- if the volume name is emptyNotFoundException
- if the volume is not foundpublic function remove(string $name, bool $force = false): bool
Removes a volume.
$name
- Volume name$force
- Force remove the volume even if it’s in use (default false
)true
if the volume was successfully removedMissingRequiredParameterException
- if the volume name is emptyNotFoundException
- if the volume is not foundOperationFailedException
- if removal failedpublic function prune(array $filters = []): array
Removes unused volumes.
$filters
- Filters for selecting volumes to clean up:
label
(string) - Remove only volumes with specified labelsall
(bool) - Remove all unused volumes, not just anonymous onesInvalidParameterValueException
- if filters are invalidOperationFailedException
- if cleanup failedpublic function exists(string $name): bool
Checks if a volume exists.
$name
- Volume nametrue
if the volume exists, false
otherwiseMissingRequiredParameterException
- if the volume name is emptyuse Sangezar\DockerClient\DockerClient;
$client = DockerClient::createUnix();
$volumes = $client->volume()->list();
foreach ($volumes['Volumes'] as $volume) {
echo "Volume: {$volume['Name']}, driver: {$volume['Driver']}\n";
}
use Sangezar\DockerClient\DockerClient;
use Sangezar\DockerClient\Config\VolumeConfig;
$client = DockerClient::createUnix();
$config = new VolumeConfig();
$config->setName('my-volume')
->setDriver(Volume::DRIVER_LOCAL)
->setLabels([
'environment' => 'development',
'application' => 'my-app'
]);
$volume = $client->volume()->create($config);
echo "Volume created with name: {$volume['Name']}\n";
use Sangezar\DockerClient\DockerClient;
$client = DockerClient::createUnix();
$volumeName = 'my-volume';
if ($client->volume()->exists($volumeName)) {
$volumeInfo = $client->volume()->inspect($volumeName);
echo "Volume: {$volumeInfo['Name']}\n";
echo "Driver: {$volumeInfo['Driver']}\n";
echo "Mount point: {$volumeInfo['Mountpoint']}\n";
if (!empty($volumeInfo['Labels'])) {
echo "Labels:\n";
foreach ($volumeInfo['Labels'] as $key => $value) {
echo " - {$key}: {$value}\n";
}
}
}
use Sangezar\DockerClient\DockerClient;
$client = DockerClient::createUnix();
$volumeName = 'my-volume';
if ($client->volume()->exists($volumeName)) {
$client->volume()->remove($volumeName);
echo "Volume {$volumeName} removed\n";
}
use Sangezar\DockerClient\DockerClient;
$client = DockerClient::createUnix();
$pruneResult = $client->volume()->prune();
echo "Volumes removed: " . count($pruneResult['VolumesDeleted'] ?? []) . "\n";
echo "Space freed: " . ($pruneResult['SpaceReclaimed'] ?? 0) . " bytes\n";