First of all you must be using a version 2 Compose file to use the new specifications for creating and using named volumes. The Compose File Reference includes all you need to know, including examples.
To summarize:
- Add
version: '2' to the top of docker-compose.yml.
- Place service units under a
services: key.
- Place volume units under a
volumes: key.
- When referring to a named volume from a service unit, specify
volumename:/path where volumename is the name given under the volumes: key (in the example below it is dbdata) and /path is the location inside the container of the mounted volume (e.g., /var/lib/mysql).
Here's a minimal example that creates a named volume dbdata and references it from the db service.
version: '2'
services:
db:
image: mysql
volumes:
- dbdata:/var/lib/mysql
volumes:
dbdata:
driver: local