SiYu Liu
SiYu Liu

Reputation: 11

How to config volumes in docker-compose.yml?

Is a very simple question I guess, but I could not find an answer. This is my docker-compose.yml

version: '3'
services:
  db:
    volumes:
      - db:/var/lib/mysql
volumes:
  db: 
  nextcloud:

The question is, I want to specify the value of "db" or "nextcloud" in "volumes", and reference them in "Services".like this

services:
  db:
    volumes:
      - db:/var/lib/mysql
  nextcloud:
    volumes:
      - nextcloud:/var/www/html
volumes:
  db: /home/roj/DataDisk/nextcloud-insecure/db
  nextcloud: /home/roj/DataDisk/nextcloud-insecure/disk

but I got problemERROR: In file './docker-compose.yml', volume 'db' must be a mapping not a string.

how can i fix it ?

Upvotes: 1

Views: 1574

Answers (2)

DannyB
DannyB

Reputation: 14846

Your syntax in the outer volumes instruction is incorrect.

If you want to mount to a docker-managed volume, do this:

services:
  test:
    image: alpine
    volumes:
      - db:/app

volumes:
  db:

If you want to mount to a local path, do this (you can replace the dot in .:/app with any other local path, like: /home/you:/server/path):

services:
  test:
    image: alpine
    volumes:
      - .:/app

If it starts with a dot or a slash, it will be treated as a path, otherwise, as a docker-managed named volume.

These are the common usage patterns, but you can read more about volumes in compose for some additional information.

Upvotes: 2

michaeldel
michaeldel

Reputation: 2385

The top-level volumes section is not meant to specify mounts but volume driver configuration (see official documention on that matter). ie. this is incorrect

volumes:
  db: /home/roj/DataDisk/nextcloud-insecure/db # incorrect
  nextcloud: /home/roj/DataDisk/nextcloud-insecure/disk # incorrect

If you want to mount host directories to you container, you must specify it in the volumes section of your services, eg.

services:
  db:
    volumes:
      - /home/roj/DataDisk/nextcloud-insecure/db:/var/lib/mysql
  nextcloud:
    volumes:
      - /home/roj/DataDisk/nextcloud-insecure/disk:/var/www/html

See official documentation on services volumes for more information on that.

Upvotes: 0

Related Questions