How to use --volume option with Docker Toolbox on Windows?

后端 未结 9 2310
北荒
北荒 2020-12-12 17:19

How can I share a folder between my Windows files and a docker container, by mounting a volume with simple --volume command using Docker Toolbox on?

I\'

相关标签:
9条回答
  • 2020-12-12 17:28

    Simply using double leading slashes worked for me on Windows 7:

    docker run --rm -v //c/Users:/data alpine ls /data/
    

    Taken from here: https://github.com/moby/moby/issues/12590

    0 讨论(0)
  • 2020-12-12 17:31

    This is an improvement of the selected answer because that answer is limited to c:\Users folder. If you want to create a volume using a directory outside of c:\Users this is an extension.

    In windows 7, I used docker toolbox. It used Virtual Box.

    1. Open virtual box
    2. Select the machine (in my case default).
    3. Right clicked and select settings option
    4. Go to Shared Folders
    5. Include a new machine folder.

    For example, in my case I have included:

    **Name**: c:\dev
    **Path**: c/dev
    
    1. Click and close
    2. Open "Docker Quickstart Terminal" and restart the docker machine.

    Use this command:

    $ docker-machine restart
    

    To verify that it worked, following these steps:

    1. SSH to the docker machine.

    Using this command:

    $ docker-machine ssh
    
    1. Go to the folder that you have shared/mounted.

    In my case, I use this command

    $ cd /c/dev
    
    1. Check the user owner of the folder. You could use "ls -all" and verify that the owner will be "docker"

    You will see something like this:

    docker@default:/c/dev$ ls -all
    total 92
    drwxrwxrwx    1 docker   staff         4096 Feb 23 14:16 ./
    drwxr-xr-x    4 root     root            80 Feb 24 09:01 ../
    drwxrwxrwx    1 docker   staff         4096 Jan 16 09:28 my_folder/
    

    In that case, you will be able to create a volume for that folder.

    You can use these commands:

    docker create -v /c/dev/:/app/dev --name dev image
    docker run -d -it --volumes-from dev image
    

    or

    docker run -d -it -v /c/dev/:/app/dev image
    

    Both commands work for me. I hope this will be useful.

    0 讨论(0)
  • 2020-12-12 17:31

    For those using Virtual Box who prefer command-line approach

    1) Make sure the docker-machine is not running

    Docker Quickstart Terminal:

    docker-machine stop
    

    2) Create the sharing Windows <-> docker-machine

    Windows command prompt:
    (Modify following to fit your scenario. I feed my Apache httpd container from directory synced via Dropbox.)

    set VBOX=D:\Program Files\Oracle\VirtualBox\VBoxManage.exe
    set VM_NAME=default
    set NAME=c/htdocs
    set HOSTPATH=%DROPBOX%\htdocs
    "%VBOX%" sharedfolder add "%VM_NAME%" --name "%NAME%" --hostpath "%HOSTPATH%" --automount
    

    3) Start the docker-machine and mount the volume in a new container

    Docker Quickstart Terminal:
    (Again, I am starting an Apache httpd container, hence that port exposing.)

    docker-machine start
    docker run -d --name my-apache-container-0 -p 80:80 -v /c/htdocs:/usr/local/apache2/htdocs my-apache-image:1.0
    
    0 讨论(0)
  • 2020-12-12 17:35

    This is actually an issue of the project and there are 2 working workarounds:

    1. Creating a data volume:

      docker create -v //c/Users/myuser:/myuser --name data hello-world
      winpty docker run -it --rm --volumes-from data ubuntu
      
    2. SSHing directly in the docker host:

      docker-machine ssh default
      

      And from there doing a classic:

      docker run -it --rm --volume /c/Users/myuser:/myuser ubuntu
      
    0 讨论(0)
  • 2020-12-12 17:35

    As of August 2016 Docker for windows now uses hyper-v directly instead of virtualbox, so I think it is a little different. First share the drive in settings then use the C: drive letter format, but use forward slashes. For instance I created an H:\t\REDIS directory and was able to see it mounted on /data in the container with this command:

    docker run -it --rm -v h:/t/REDIS:/data redis sh
    

    The same format, using drive letter and a colon then forward slashes for the path separator worked both from windows command prompt and from git bash.

    I found this question googling to find an answer, but I couldn't find anything that worked. Things would seem to work with no errors being thrown, but I just couldn't see the data on the host (or vice-versa). Finally I checked out the settings closely and tried the format they show:

    So first, you have to share the whole drive to the docker vm in settings here, I think that gives the 'docker-machine' vm running in hyper-v access to that drive. Then you have to use the format shown there, which seems to only exist in this one image and in no documentation or questions I could find on the web:

    docker run --rm -v c:/Users:/data alpine ls /data
    
    0 讨论(0)
  • 2020-12-12 17:36

    Try this:

    1. Open Docker Quickstart Terminal. If it is already open, run $ cd ~ to make sure you are in Windows user directory.
    2. $ docker run -it -v /$(pwd)/ubuntu:/windows ubuntu

    It will work if the error is due to typo. You will get an empty folder named ubuntu in your user directory. You will see this folder with the name windows in your ubuntu container.

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