How to mount local volumes in docker machine

后端 未结 12 1412
天涯浪人
天涯浪人 2020-11-27 10:05

I am trying to use docker-machine with docker-compose. The file docker-compose.yml has definitions as follows:

web:
  build: .
  command: ./run_web.sh
  volu         


        
12条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 10:54

    Finally figured out how to upgrade Windows Docker Toolbox to v1.12.5 and keep my volumes working by adding a shared folder in Oracle VM VirtualBox manager and disabling path conversion. If you have Windows 10+ then you're best to use the newer Docker for Windows.

    1st the upgrade Pain:

    1. Uninstall VirtualBox first.
      • Yep that may break stuff in other tools like Android Studio. Thanks Docker :(
    2. Install new version of Docker Toolbox.

    Redis Database Example: redis: image: redis:alpine container_name: redis ports: - "6379" volumes: - "/var/db/redis:/data:rw"

    In Docker Quickstart Terminal ....

    1. run docker-machine stop default - Ensure VM is haulted

    In Oracle VM VirtualBox Manager ...

    1. Added a shared folder in default VM via or command line
      • D:\Projects\MyProject\db => /var/db

    In docker-compose.yml...

    1. Mapped redis volume as: "/var/db/redis:/data:rw"

    In Docker Quickstart Terminal ....

    1. Set COMPOSE_CONVERT_WINDOWS_PATHS=0 (for Toolbox version >= 1.9.0)
    2. run docker-machine start default to restart the VM.
    3. cd D:\Projects\MyProject\
    4. docker-compose up should work now.

    Now creates redis database in D:\Projects\MyProject\db\redis\dump.rdb

    Why avoid relative host paths?

    I avoided relative host paths for Windows Toolbox as they may introduce invalid '\' chars. It's not as nice as using paths relative to docker-compose.yml but at least my fellow developers can easily do it even if their project folder is elsewhere without having to hack the docker-compose.yml file (bad for SCM).

    Original Issue

    FYI ... Here is the original error I got when I used nice clean relative paths that used to work just fine for older versions. My volume mapping used to be just "./db/redis:/data:rw"

    ERROR: for redis Cannot create container for service redis: Invalid bind mount spec "D:\\Projects\\MyProject\\db\\redis:/data:rw": Invalid volume specification: 'D:\Projects\MyProject\db\redis:/data

    This breaks for two reasons ..

    1. It can't access D: drive
    2. Volume paths can't include \ characters
      • docker-compose adds them and then blames you for it !!
      • Use COMPOSE_CONVERT_WINDOWS_PATHS=0 to stop this nonsense.

    I recommend documenting your additional VM shared folder mapping in your docker-compose.yml file as you may need to uninstall VirtualBox again and reset the shared folder and anyway your fellow devs will love you for it.

提交回复
热议问题