Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replicate 'docker volume create --name data' command on docker-compose.yml

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:

  • Create a volume, or use it if already created.
  • Create services containers with data from previous volume container.
like image 856
Juan C. Irizar Avatar asked Dec 29 '15 15:12

Juan C. Irizar


People also ask

Does Docker compose create volume?

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.


1 Answers

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:

  1. Add version: '2' to the top of docker-compose.yml.
  2. Place service units under a services: key.
  3. Place volume units under a volumes: key.
  4. 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
like image 130
Quinn Comendant Avatar answered Nov 02 '22 22:11

Quinn Comendant