I'm building my containers with docker-compose
and I would like to use the new volume API from Docker, but I don't see how to.
I want to be able to say docker-compose up -d
to:
Here's an example of a single Docker Compose service with a volume: services: frontend: image: node:lts volumes: - myapp:/home/node/app volumes: myapp: Running docker compose up for the first time creates a volume. The same volume is reused when you subsequently run the command.
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:
version: '2'
to the top of docker-compose.yml
.services:
key.volumes:
key.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With