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 = deb3cc18e8eac5585dca795cfb5824b90a0fa08b451dda5a19a2da9ecf5cbaad

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/deb3cc18e8eac5585dca795cfb5824b90a0fa08b451dda5a19a2da9ecf5cbaad/_data

In [7]: super_print(volume.created_at)
type = <class 'datetime.datetime'>, value = 2022-08-18 13:47:11+00:00

In [8]: super_print(volume.status)
type = <class 'NoneType'>, value = None

In [9]: super_print(volume.labels)
type = <class 'dict'>, value = {}

In [10]: super_print(volume.scope)
type = <class 'str'>, value = local

In [11]: super_print(volume.options)
type = <class 'dict'>, value = {}

Methods

clone

Volume.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

Volume.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.


reload

Volume.reload()

remove

Volume.remove()

Removes this volume