The VolumeConfig
class is part of the Sangezar\DockerClient\Config
namespace and provides a convenient interface for configuring and creating Docker volumes.
namespace Sangezar\DockerClient\Config;
The VolumeConfig
class allows you to create and configure Docker volume configurations that can be used by the API client to create new volumes.
$volumeConfig = VolumeConfig::create();
Static method for creating a new volume configuration instance.
$volumeConfig = VolumeConfig::create();
Sets the volume name.
Parameters:
$name
- Volume nameExceptions:
InvalidParameterValueException
- if the volume name is empty or invalid.$volumeConfig->setName('my-volume');
Sets the volume driver.
Parameters:
$driver
- Volume driver (e.g., ‘local’, ‘nfs’, ‘cifs’, etc.)Exceptions:
InvalidParameterValueException
- if the driver parameter is empty.$volumeConfig->setDriver('local');
Adds a driver option.
Parameters:
$key
- Option key$value
- Option value$volumeConfig->addDriverOpt('type', 'nfs');
$volumeConfig->addDriverOpt('device', ':/path/to/dir');
$volumeConfig->addDriverOpt('o', 'addr=192.168.1.1,rw');
Configures an NFS volume. Automatically sets the driver to ‘local’ and adds the appropriate options.
Parameters:
$serverAddress
- IP address or hostname of the NFS server$serverPath
- Path on the NFS server to mount$options
- Mounting options that will be added to the parameters stringExceptions:
InvalidParameterValueException
- if the server address or path is empty.$volumeConfig->setupNfs('192.168.1.100', '/exports/data');
Adds a label to the volume.
Parameters:
$key
- Label key$value
- Label value$volumeConfig->addLabel('environment', 'production');
$volumeConfig->addLabel('backup', 'weekly');
Converts the configuration to an array for the Docker API.
Exceptions:
InvalidConfigurationException
- if the configuration is invalid.Returns:
array<string, mixed>
- Configuration array for the Docker API.$configArray = $volumeConfig->toArray();
use Sangezar\DockerClient\Config\VolumeConfig;
// Creating a volume configuration
$volumeConfig = VolumeConfig::create()
->setName('app-data')
->setDriver('local')
->addLabel('application', 'my-app')
->addLabel('environment', 'development');
// Creating a volume using the API client
$dockerClient->volumes()->create($volumeConfig);
use Sangezar\DockerClient\Config\VolumeConfig;
// Creating an NFS volume
$volumeConfig = VolumeConfig::create()
->setName('shared-data')
->setupNfs('192.168.1.100', '/exports/shared', 'addr={server},rw,nolock,soft')
->addLabel('type', 'shared-storage');
// Creating a volume using the API client
$dockerClient->volumes()->create($volumeConfig);
use Sangezar\DockerClient\Config\VolumeConfig;
// Creating a volume with advanced options
$volumeConfig = VolumeConfig::create()
->setName('database-data')
->setDriver('local')
->addDriverOpt('type', 'tmpfs')
->addDriverOpt('device', 'tmpfs')
->addDriverOpt('o', 'size=100m,uid=1000')
->addLabel('service', 'database')
->addLabel('backup', 'hourly');
// Converting to an array for the Docker API
$configArray = $volumeConfig->toArray();
// Creating a volume using the API client
$dockerClient->volumes()->create($volumeConfig);