41

Question: Why do I get this error?

ERROR: In file './docker-compose.yml', volume 'mariavolume' must be a mapping not a string.

My docker-compose file is almost identical to this one: https://docs.docker.com/compose/wordpress/

version: '2'
services:
  wordpress:
    image: wordpress:latest
    restart: always
    depends_on:
      - db
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_PASSWORD: example
      WORDPRESS_DB_HOST: 3306
  db:
    image: mariadb
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example
    volumes:
      - maria_volume: /var/lib/mysql
volumes:
  maria_volume: ~/mariadb
StackzOfZtuff
  • 2,534
  • 1
  • 28
  • 25
Richard
  • 14,798
  • 21
  • 70
  • 103

9 Answers9

23

In my case this was happening because I missed adding a : after the volume name.

Instead of:

volumes:
    - mysqldata:

I had typed:

volumes:
    - mysqldata

docker-compose up gave me the same error as above.

Dhiraj Gupta
  • 9,704
  • 8
  • 49
  • 54
11

I've just tackled this issue myself. If you just want a volume to store data, then do as below. This will create/reuse a volume that is persisted to disk as part of the Docker graph driver.

The next question is where is this?.

You can find it inside the docker image - Default Location -

C:\Users\Public\Documents\Hyper-V\Virtual hard disks

db:
  image: mariadb
  restart: always
  environment:
    MYSQL_ROOT_PASSWORD: example
  volumes:
    - maria_volume: /var/lib/mysql

volumes:
  maria_volume:

Of course, if you are after mapping a host directory into docker rather than having it inside the Docker graph driver. Then you can do it as follows.

db:
  image: mariadb
  restart: always
  environment:
    MYSQL_ROOT_PASSWORD: example
  volumes:
    - maria_volume: /var/lib/mysql

volumes:
  maria_volume:
    driver: local
    driver_opts:
      o: bind
      type: none
      device: /C/mariadb

Please note, when mapping over host directories as volume (at least on windows) you can have issues with read/write permissions, something I have yet to resolve myself.

Phoenix
  • 3,996
  • 4
  • 29
  • 40
JonWillis
  • 3,146
  • 5
  • 32
  • 54
  • Thank you, it's worked for me :) you saved my day :) – web hobbit Oct 09 '19 at 16:37
  • Hi, just wondering if you find a solution for the read/write permissions issues on Windows, thanks. – bouchepat May 11 '21 at 19:27
  • @bouchepat I found on Windows that moving your data out of the WSL linux partition and onto your drive e.g. /c/docker/data fixed read/write permissions for me – Baxny May 18 '21 at 00:25
  • This worked for me, but I am using Linux, normally this is `/var/lib/docker` and there is the folder volumes. To find your docker root dir just type `docker info | grep "Docker Root Dir"` – Gilberto Treviño Jun 08 '21 at 14:19
10

Unfortunately, there is no such a feature.

You can’t map a top-level volume in docker-compose.

Here are the options:

  • Adding volume per container and map it there. (like what Daniel did here)
  • Create a volume outside of the compose (with mapping) and use it in your compose.

    volumes:
       maria_volume: 
           external:
               name: volume-name
    
Community
  • 1
  • 1
DaNeSh
  • 1,022
  • 1
  • 14
  • 24
5

try this:

    volumes:
        - maria_volume: /var/lib/mysql
volumes:
    maria_volume: 
        external:
            name: ~/mariadb
Alison R.
  • 4,204
  • 28
  • 33
2

correct syntax for volume is
volumes: first:
where first is just a placeholder name to be used for volume

Akshay Vijay Jain
  • 13,461
  • 8
  • 60
  • 73
1

Try this:

version: '2'
services:
  ...
  db:
    image: mariadb
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example
    volumes:
      - ~/mariadb:/var/lib/mysql
kinjelom
  • 6,105
  • 3
  • 35
  • 61
1

try this, it works for me

   volumes:
     - maria_volume: /var/lib/mysql

volumes:
  maria_volume: 
    drive: local
Fahd Rahali
  • 451
  • 4
  • 6
0

I was running into the same issue as yourself and as a last act of despair I tried putting

volumes:
  - maria_volume: /var/lib/mysql

before

environment:
  MYSQL_ROOT_PASSWORD: example

I'm not sure what kind of magic applied here but in my case, it worked

Let me know!

Daniel
  • 1,172
  • 14
  • 31
  • 1
    Thanks, yes this works, but then you're no longer using a single named volume throughout your compose file, you're manually doing it per service version 1 docker-compose style hardcoded to the folder – Richard Dec 29 '16 at 11:13
-1

For me this works:

In #docker_compose.yml:

volumes:
  postgres_data: {}
  static: { }
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Deepak Sharma
  • 1,401
  • 19
  • 21