How to mount local volumes in docker machine

后端 未结 12 1358
天涯浪人
天涯浪人 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 11:00

    It can be done witch combination of three tools: docker-machine mount, rsync, inotifywait

    TL;DR

    Script based on all below is here

    Let's say you have your docker-compose.yml and run_web.sh in /home/jdcaballerov/web

    1. Mount directory on machine which has same path as you have it on your host docker-machine machine:/home/jdcaballerov/web /tmp/some_random_dir
    2. Synchronize mounted directory with dir on your host rsync -r /home/jdcaballerov/web /tmp/some_random_dir
    3. Synchronize on every change of files in your directory:

      inotifywait -r -m -e close_write --format '%w%f' /home/jdcaballerov/web | while read CHANGED_FILE
      do
          rsync /home/jdcaballerov/web /tmp/some_random_dir
      done
      

    BE AWARE - there are two directories which has same path - one is on your local (host) machine, second is on docker machine.

    0 讨论(0)
  • 2020-11-27 11:01

    I am using docker-machine 0.12.2 with the virtualbox drive on my local machine. I found that there is a directory /hosthome/$(user name) from where you have access to local files.

    0 讨论(0)
  • 2020-11-27 11:07

    Also ran into this issue and it looks like local volumes are not mounted when using docker-machine. A hack solution is to

    1. get the current working directory of the docker-machine instance docker-machine ssh <name> pwd

    2. use a command line tool like rsync to copy folder to remote system

      rsync -avzhe ssh --progress <name_of_folder> username@remote_ip:<result _of_pwd_from_1>.
      

    The default pwd is /root so the command above would be rsync -avzhe ssh --progress <name_of_folder> username@remote_ip:/root

    NB: you would need to supply the password for the remote system. You can quickly create one by ssh into the remote system and creating a password.

    1. change the volume mount point in your docker-compose.yml file from .:/app to /root/<name_of_folder>:/app

    2. run docker-compose up -d

    NB when changes are made locally, don't forget to rerun rsync to push the changes to the remote system.

    Its not perfect but it works. An issue is ongoing https://github.com/docker/machine/issues/179

    Other project that attempt to solve this include docker-rsync

    0 讨论(0)
  • 2020-11-27 11:10

    All other answers were good for the time but now (Docker Toolbox v18.09.3) all works out of the box. You just need to add a shared folder into VirtualBox VM.

    Docker Toolbox automatically adds C:\Users as shared folder /c/Users under virtual linux machine (using Virtual Box shared folders feature), so if your docker-compose.yml file is located somewhere under this path and you mount host machine's directories only under this path - all should work out of the box.

    For example:

    C:\Users\username\my-project\docker-compose.yml:

    ...
      volumes:
        - .:/app
    ...
    

    The . path will be automatically converted to absolute path C:\Users\username\my-project and then to /c/Users/username/my-project. And this is exactly how this path is seen from the point of view of linux virtual machine (you can check it: docker-machine ssh and then ls /c/Users/username/my-project). So, the final mount will be /c/Users/username/my-project:/app.

    All works transparently for you.

    But this doesn't work if your host mount path is not under C:\Users path. For example, if you put the same docker-compose.yml under D:\dev\my-project.

    This can be fixed easily though.

    1. Stop the virtual machine (docker-machine stop).
    2. Open Virtual Box GUI, open Settings of Virtual Machine named default, open Shared Folders section and add the new shared folder:

      • Folder Path: D:\dev
      • Folder Name: d/dev

      Press OK twice and close Virtual Box GUI.

    3. Start the virtual machine (docker-machine start).

    That's all. All paths of host machine under D:\dev should work now in docker-compose.yml mounts.

    0 讨论(0)
  • 2020-11-27 11:11

    Since October 2017 there is a new command for docker-machine that does the trick, but make sure there is nothing in the directory before executing it, otherwise it might get lost:

    docker-machine mount <machine-name>:<guest-path> <host-path>

    Check the docs for more information: https://docs.docker.com/machine/reference/mount/

    PR with the change: https://github.com/docker/machine/pull/4018

    0 讨论(0)
  • 2020-11-27 11:11

    If you choose the rsync option with docker-machine, you can combine it with the docker-machine ssh <machinename> command like this:

    rsync -rvz --rsh='docker-machine ssh <machinename>' --progress <local_directory_to_sync_to> :<host_directory_to_sync_to>

    It uses this command format of rsync, leaving HOST blank:

    rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST

    (http://linuxcommand.org/man_pages/rsync1.html)

    0 讨论(0)
提交回复
热议问题