How do I mount a Docker volume while using a Windows host?

后端 未结 3 1256
后悔当初
后悔当初 2020-12-05 08:37

Mounting a Docker volume while being under a Windows host, has been a huge pain for me, and I could not get it to work.

Currently I got the following simple Dockerfi

相关标签:
3条回答
  • 2020-12-05 09:04

    It is possible the / is interpreted as an option by the CMD Windows shell.

    Try first a docker-machine ssh default, in order to open an ssh session in your VM. From there try the docker run again: docker run -v /c/Users/phisch/dev/htdocs:/var/www phisch:dev

    As commented by thaJeztah in issue 18290:

    You could consider using docker-compose; docker-compose allows you to define bind-mounted volumes relative to the location of the docker-compose.yml file.
    Using a docker-compose file allows you to specify all options needed to run your containers in a single file, which makes it ideal for sharing between team members (ie, just run docker-compose up -d will start all containers for the project with the right options).

    This comment mentions a&dding a second /:

    docker run -v //c/Users/phisch/dev/htdocs:`/var/www` phisch:dev
    

    Even in the docker toolbox msys shell session, there are issues (like issue 282)


    After lengthy discussion, the issue was that /var/www had a folder in it.

    Mounting /c/Users/phisch/dev/htdoc onto an empty folder does work, but might not give the expected result, as the default CMD apache2-foreground might still serve its content based on /var/www (which would not have htdocs content if that htdocs is mounted onto another folder).

    0 讨论(0)
  • 2020-12-05 09:09

    I use Docker for Windows with PowerShell and use $PWD to refer to the current directory when mounting a volume, and it works well for me. A couple of examples:

    docker run -p 2368:2368 -v $PWD/ghost:/var/lib/ghost -d ghost
    
    docker run -p 2368:2368 -v $PWD/:/var/lib/ghost -d ghost
    
    0 讨论(0)
  • 2020-12-05 09:12

    If we are talking about Docker on Windows then we have to take in account the fact that all containers are run on VirtualBox.

    Before mounting volume to a container we have to be sure that particular folder is available for VirtualBox.

    Firstly, to define the name of the current running Docker machine, run

    $ docker-machine.exe  active
    default
    

    Secondly, add shared folder to VirtualBox:

    $ VBoxManage sharedfolder add default --name "some_project" --hostpath D:\Projects\some_project
    

    Thirdly, create the folder

    $ docker-machine.exe ssh default 'sudo mkdir --parents /d/projects/some_project'
    

    Fourthly, mount it:

    $ docker-machine.exe ssh default 'sudo mount -t vboxsf some_project /d/projects/some_project'
    

    Finally, create a container:

    $ docker run -v //d/projects/some_project://d/projects/some_project -d some-image_name
    
    0 讨论(0)
提交回复
热议问题