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

六眼飞鱼酱① 提交于 2019-11-29 03:18:40
DaNeSh

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
    

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.

Mohsen ZareZardeyni

try this:

    volumes:
        - maria_volume: /var/lib/mysql
volumes:
    maria_volume: 
        external:
            name: ~/mariadb

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!

Deepak Sharma

For me this works:

In #docker_compose.yml:

volumes:
  postgres_data: {}
  static: { }

Try this:

version: '2'
services:
  ...
  db:
    image: mariadb
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example
    volumes:
      - ~/mariadb:/var/lib/mysql

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 which is persisted to disk as part of the docker graph driver. 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!