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

核能气质少年 提交于 2019-11-27 09:31:22

问题


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'm using "Docker Quickstart Terminal" and when I try this:

winpty docker run -it --rm --volume /C/Users/myuser:/myuser ubuntu

I have this error:

Invalid value "C:\\Users\\myuser\\:\\myuser" for flag --volume: bad mount mode specified : \myuser
See 'docker run --help'.

Following this, I also tried

winpty docker run -it --rm --volume "//C/Users/myuser:/myuser" ubuntu

and got

Invalid value "\\\\C:\\Users\\myuser\\:\\myuser" for flag --volume: \myuser is not an absolute path
See 'docker run --help'.

回答1:


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.




回答2:


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
    



回答3:


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



回答4:


If you are looking for the solution that will resolve all the Windows issues and make it work on the Windows OS in the same way as on Linux, then see below. I tested this and it works in all cases. I’m showing also how I get it (the steps and thinking process). I've also wrote an article about using Docker and dealing with with docker issues here.

Solution 1: Use VirtualBox (if you think it's not good idea see Solution 2 below)

  • Open VirtualBox (you have it already installed along with the docker tools)
  • Create virtual machine
  • (This is optional, you can skip it and forward ports from the VM) Create second ethernet card - bridged, this way it will receive IP address from your network (it will have IP like docker machine)
  • Install Ubuntu LTS which is older than 1 year
  • Install docker
  • Add shared directories to the virtual machine and automount your project directories (this way you have access to the project directory from Ubuntu) but still can work in Windows
  • Done

Bonus:

  • Everything is working the same way as on Linux
  • Pause/Unpause the dockerized environment whenever you want

Solution 2: Use VirtualBox (this is very similar to the solution 1 but it shows also the thinking process, which might be usefull when solving similar issues)

  • Read that somebody move the folders to /C/Users/Public and that works https://forums.docker.com/t/sharing-a-volume-on-windows-with-docker-toolbox/4953/2
  • Try it, realize that it doesn’t have much sense in your case.
  • Read entire page here https://github.com/docker/toolbox/issues/607 and try all solutions listed on page
  • Find this page (the one you are reading now) and try all the solutions from other comments
  • Find somewhere information that setting COMPOSE_CONVERT_WINDOWS_PATHS=1 environment variable might solve the issue.
  • Stop looking for the solution for few months
  • Go back and check the same links again
  • Cry deeply
  • Feel the enlightenment moment
  • Open VirtualBox (you have it already installed along with the docker tools)
  • Create virtual machine with second ethernet card - bridged, this way it will receive IP address from your network (it will have IP like docker machine)
  • Install Ubuntu LTS which is very recent (not older than few months)
  • Notice that the automounting is not really working and the integration is broken (like clipboard sharing etc.)
  • Delete virtual machine
  • Go out and have a drink
  • Rent expensive car and go with high speed on highway
  • Destroy the car and die
  • Respawn in front of your PC
  • Install Ubuntu LTS which is older than 1 year
  • Try to run docker
  • Notice it’s not installed
  • Install docker by apt-get install docker
  • Install suggested docker.io
  • Try to run docker-compose
  • Notice it’s not installed
  • apt get install docker-compose
  • Try to run your project with docker-compose
  • Notice that it’s old version
  • Check your power level (it should be over 9000)
  • Search how to install latest version of docker and find the official guide https://docs.docker.com/install/linux/docker-ce/ubuntu/
  • Uninstall the current docker-compose and docker.io
  • Install docker using the official guide https://docs.docker.com/install/linux/docker-ce/ubuntu/
  • Add shared directories to the virtual machine and automount your project directories (this way you have access to the project directory from Ubuntu, so you can run any docker command)
  • Done



回答5:


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.




回答6:


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



回答7:


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




回答8:


share folders virtualBox toolbox and windows 7 and nodejs image container

using... Docker Quickstart Terminal [QST] Windows Explorer [WE]

lets start...

  1. [QST] open Docker Quickstart Terminal

  2. [QST] stop virtual-machine

$ docker-machine stop

  1. [WE] open a windows explorer

  2. [WE] go to the virtualBox installation dir

  3. [WE] open a cmd and execute...

C:\Program Files\Oracle\VirtualBox>VBoxManage sharedfolder add "default" --name "/d/SVN_FOLDERS/X2R2_WP6/nodejs" --hostpath "\?\d:\SVN_FOLDERS\X2R2_WP6\nodejs" --automount

  1. check in the oracle virtual machine, that the new shared folder has appeared

  2. [QST] start virtual-machine

$ docker-machine start

  1. [QST] run container nodejs

docker stop nodejs

docker rm nodejs

docker run -d -it --rm --name nodejs -v /d/SVN_FOLDERS/X2R2_WP6/nodejs:/usr/src/app -w /usr/src/app node2

  1. [QST] open bash to the container

docker exec -i -t nodejs /bin/bash

  1. [QST] execute dir and you will see the shared files



回答9:


I solved it!

Add a volume:

docker run -d -v my-named-volume:C:\MyNamedVolume testimage:latest

Mount a host directory:

docker run -d -v C:\Temp\123:C:\My\Shared\Dir testimage:latest



来源:https://stackoverflow.com/questions/33126271/how-to-use-volume-option-with-docker-toolbox-on-windows

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