A Python library for the Docker Engine API.
—
Docker volume operations for persistent data storage including volume creation, mounting, and lifecycle management with support for volume drivers and custom configurations.
class VolumeCollection:
def create(self, name=None, **kwargs):
"""
Create a volume.
Args:
name (str): Volume name (auto-generated if None)
**kwargs: Volume configuration options
Returns:
Volume: Created volume instance
Common kwargs:
driver (str): Volume driver (default: 'local')
driver_opts (dict): Driver-specific options
labels (dict): Volume labels
"""
def get(self, volume_id):
"""
Get a volume by name.
Args:
volume_id (str): Volume name
Returns:
Volume: Volume instance
"""
def list(self, filters=None):
"""
List volumes.
Args:
filters (dict): Filter results by name, driver, label, etc.
Returns:
list[Volume]: List of volume instances
"""
def prune(self, filters=None):
"""
Remove unused volumes.
Args:
filters (dict): Filters for pruning
Returns:
dict: Pruning results with space reclaimed
"""class Volume:
"""
A Docker volume instance.
Properties:
id (str): Volume ID
name (str): Volume name
attrs (dict): Raw volume attributes
"""
def remove(self, force=False):
"""
Remove the volume.
Args:
force (bool): Force removal even if volume is in use
"""import docker
client = docker.from_env()
# Create named volume
volume = client.volumes.create(
name='app-data',
driver='local',
driver_opts={'type': 'tmpfs', 'device': 'tmpfs', 'o': 'size=100m'}
)
# Use volume in container
container = client.containers.run(
'postgres:13',
environment={'POSTGRES_PASSWORD': 'secret'},
volumes={'app-data': {'bind': '/var/lib/postgresql/data', 'mode': 'rw'}},
detach=True
)
# List and inspect volumes
for vol in client.volumes.list():
print(f"Volume: {vol.name}, Driver: {vol.attrs['Driver']}")