ScanSkill

Compose Volume Configurations

Although you can build named volumes that can be reused across several services (without relying on volumes from) and are readily retrieved and inspected using the docker command line or API, you can also declare volumes on the fly as part of the service declaration.

driver

Name the volume driver that should be applied to this volume. 'local' is the default. If the driver is unavailable, the Docker Engine will return an error.

 driver: foobar

driver_opts

Give the volume's driver a list of options as key-value pairs to work with. These choices rely on the driver; for further details, refer to the driver's documentation. (Optional)

 driver_opts:
   foo: "bar"
   baz: 1

external

If set to true, specifies that this volume has been created outside of Compose. docker-compose up will not attempt to create it, and will raise an error if it doesn’t exist.

external cannot be used in conjunction with other volume configuration keys (driverdriver_opts).

In the example below, instead of attempting to create a volume called [projectname]_data, Compose will look for an existing volume simply called data and mount it into the db service’s containers.

This indicates that this volume was generated outside of Compose if set to true. If it doesn't already exist, docker-compose up won't try to create it and will raise an error instead.

Excluding driver and driver opts, the external cannot be used with other volume configuration keys.

In the example below, Compose will search for an existing volume just called data and mount it into the containers for the database service rather than attempting to construct a volume called [projectname] data.

version: '2'

services:
  db:
    image: postgres
    volumes:
      - data:/var/lib/postgresql/data

volumes:
  data:
    external: true

You can also specify the name of the volume separately from the name used to refer to it within the Compose file:

volumes:
  data:
    external:
      name: actual-name-of-volume