Volumes

Manage volumes on the server.

Methods available on client.volumes:

class VolumeCollection
create(name=None, **kwargs)

Create a volume.

Parameters
  • name (str) – Name of the volume. If not specified, the engine generates a name.

  • driver (str) – Name of the driver used to create the volume

  • driver_opts (dict) – Driver options as a key-value dictionary

  • labels (dict) – Labels to set on the volume

Returns

The volume created.

Return type

(Volume)

Raises

docker.errors.APIError – If the server returns an error.

Example

>>> volume = client.volumes.create(name='foobar', driver='local',
        driver_opts={'foo': 'bar', 'baz': 'false'},
        labels={"key": "value"})
get(volume_id)

Get a volume.

Parameters

volume_id (str) – Volume name.

Returns

The volume.

Return type

(Volume)

Raises
  • docker.errors.NotFound – If the volume does not exist.

  • docker.errors.APIError – If the server returns an error.

list(**kwargs)

List volumes. Similar to the docker volume ls command.

Parameters

filters (dict) – Server-side list filtering options.

Returns

The volumes.

Return type

(list of Volume)

Raises

docker.errors.APIError – If the server returns an error.

prune(filters=None)

Delete unused volumes

Parameters

filters (dict) – Filters to process on the prune list.

Returns

A dict containing a list of deleted volume names and

the amount of disk space reclaimed in bytes.

Return type

(dict)

Raises

docker.errors.APIError – If the server returns an error.

Volume objects

class Volume

A volume.

id

The ID of the object.

short_id

The ID of the object, truncated to 12 characters.

name

The name of the volume.

attrs

The raw representation of this object from the server.

reload()

Load this object from the server again and update attrs with the new data.

remove(force=False)

Remove this volume.

Parameters

force (bool) – Force removal of volumes that were already removed out of band by the volume driver plugin.

Raises

docker.errors.APIError – If volume failed to remove.