0
# Volume Management
1
2
Docker volume operations for persistent data storage including volume creation, mounting, and lifecycle management with support for volume drivers and custom configurations.
3
4
## Capabilities
5
6
### VolumeCollection
7
8
```python { .api }
9
class VolumeCollection:
10
def create(self, name=None, **kwargs):
11
"""
12
Create a volume.
13
14
Args:
15
name (str): Volume name (auto-generated if None)
16
**kwargs: Volume configuration options
17
18
Returns:
19
Volume: Created volume instance
20
21
Common kwargs:
22
driver (str): Volume driver (default: 'local')
23
driver_opts (dict): Driver-specific options
24
labels (dict): Volume labels
25
"""
26
27
def get(self, volume_id):
28
"""
29
Get a volume by name.
30
31
Args:
32
volume_id (str): Volume name
33
34
Returns:
35
Volume: Volume instance
36
"""
37
38
def list(self, filters=None):
39
"""
40
List volumes.
41
42
Args:
43
filters (dict): Filter results by name, driver, label, etc.
44
45
Returns:
46
list[Volume]: List of volume instances
47
"""
48
49
def prune(self, filters=None):
50
"""
51
Remove unused volumes.
52
53
Args:
54
filters (dict): Filters for pruning
55
56
Returns:
57
dict: Pruning results with space reclaimed
58
"""
59
```
60
61
### Volume Model
62
63
```python { .api }
64
class Volume:
65
"""
66
A Docker volume instance.
67
68
Properties:
69
id (str): Volume ID
70
name (str): Volume name
71
attrs (dict): Raw volume attributes
72
"""
73
74
def remove(self, force=False):
75
"""
76
Remove the volume.
77
78
Args:
79
force (bool): Force removal even if volume is in use
80
"""
81
```
82
83
## Usage Examples
84
85
```python
86
import docker
87
88
client = docker.from_env()
89
90
# Create named volume
91
volume = client.volumes.create(
92
name='app-data',
93
driver='local',
94
driver_opts={'type': 'tmpfs', 'device': 'tmpfs', 'o': 'size=100m'}
95
)
96
97
# Use volume in container
98
container = client.containers.run(
99
'postgres:13',
100
environment={'POSTGRES_PASSWORD': 'secret'},
101
volumes={'app-data': {'bind': '/var/lib/postgresql/data', 'mode': 'rw'}},
102
detach=True
103
)
104
105
# List and inspect volumes
106
for vol in client.volumes.list():
107
print(f"Volume: {vol.name}, Driver: {vol.attrs['Driver']}")
108
```