Docker volumes
Don't use the constructor directly. Instead use
from python_on_whales import docker
my_docker_volume = docker.volume.inspect("my-volume")
# or
my_docker_image = docker.volume.create()
For type hints, use this
from python_on_whales import docker, Volume
def print_file(my_volume: Volume):
print(
docker.run(
"ubuntu",
["cat", "/some_volume/some_file"],
volumes=[(my_volume, "/some_volume/some_file")]
)
)
Attributes
It attributes are the same that you get with the command line:
docker volume inspect ...
To get a complete description of those attributes, you can take a look at the daemon api reference page and click on "200 No error".
An example is worth many lines of descriptions.
In [1]: from python_on_whales import docker
In [2]: volume = docker.volume.create()
In [3]: def super_print(obj):
...: print(f"type={type(obj)}, value={obj}")
...:
In [4]: super_print(volume.name)
type = <class 'str'>, value = ce40463546bbffe8f738dfe231f8a956fc89b4b0cddb39a225ea331d9daf38c3
In [5]: super_print(volume.driver)
type = <class 'str'>, value = local
In [6]: super_print(volume.mountpoint)
type = <class 'pathlib.PosixPath'>, value = /var/lib/docker/volumes/ce40463546bbffe8f738dfe231f8a956fc89b4b0cddb39a225ea331d9daf38c3/_data
In [7]: super_print(volume.created_at)
type = <class 'datetime.datetime'>, value = 2025-03-27 11:16:37+00:00
In [8]: super_print(volume.status)
type = <class 'NoneType'>, value = None
In [9]: super_print(volume.labels)
type = <class 'dict'>, value = {'com.docker.volume.anonymous': ''}
In [10]: super_print(volume.scope)
type = <class 'str'>, value = local
In [11]: super_print(volume.options)
type = <class 'NoneType'>, value = None
Methods
Volume
clone
clone(new_volume_name=None, driver=None, labels={}, options={})
Creates a new volume and copy all the data inside.
See the docker.volume.clone
command for
information about the arguments.
exists
exists()
Returns True
if the docker volume exists and False
if it doesn't exists.
If it doesn't exists, it most likely mean that it was removed.
See the docker.volume.exists
command for information about the arguments.
remove
remove()
Removes this volume