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

9

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
Gibbeon answered 26/12, 2016 at 17:37 Comment(0)
P
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.

Preuss answered 23/4, 2018 at 10:52 Comment(0)
K
11

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
    
Krisha answered 8/3, 2017 at 21:8 Comment(1)
This is not true, you can map the top-level volume as well.Flexile
B
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.

Borroff answered 9/5, 2019 at 17:16 Comment(4)
Thank you, it's worked for me :) you saved my day :)Joinder
Hi, just wondering if you find a solution for the read/write permissions issues on Windows, thanks.Deficiency
@Deficiency 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 meCuenca
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"Tracheostomy
M
5

try this:

    volumes:
        - maria_volume: /var/lib/mysql
volumes:
    maria_volume: 
        external:
            name: ~/mariadb
Metalware answered 27/12, 2016 at 6:59 Comment(2)
Thanks, but that gives me this syntax error: volumes.maria_volume.external contains an invalid type, it should be a boolean, or an object Gibbeon
space is important: name:<space>...Quoit
P
2

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

Propjet answered 19/11, 2019 at 11:44 Comment(0)
Z
1

Try this:

version: '2'
services:
  ...
  db:
    image: mariadb
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example
    volumes:
      - ~/mariadb:/var/lib/mysql
Zomba answered 6/8, 2018 at 9:56 Comment(0)
S
1

try this, it works for me

   volumes:
     - maria_volume: /var/lib/mysql

volumes:
  maria_volume: 
    drive: local
Selfregulating answered 17/3, 2021 at 12:28 Comment(0)
L
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!

Leonardaleonardi answered 27/12, 2016 at 13:40 Comment(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 folderGibbeon
P
-1

For me this works:

In #docker_compose.yml:

volumes:
  postgres_data: {}
  static: { }
Phlogopite answered 25/11, 2017 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.