Docker / Windows Container: how to mount a host folder as data volume on Windows 2016

46,515

Solution 1

On Windows, the paths must be specified using Windows-style semantics. You should not use a leading slash in front of the path.

docker run -v c:\Users\[path]:c:\[containerPath]

Solution 2

Windows 10 Anniversary Update and Windows Server 2016 RTM.

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
Share:
46,515

Related videos on Youtube

Mathias Conradt
Author by

Mathias Conradt

Cybersecurity Professional (CIAM, AppSec, DevSecOps, OSINT), Software Engineer, Open Source Enthusiast, Privacy Advocate, Tactical & Stealth Gamer, Motor Biker.

Updated on September 18, 2022

Comments

  • Mathias Conradt
    Mathias Conradt over 1 year

    How to mount a volume from Windows host to Windows guest system?

    I am on Windows Server 2016 TP4 using Docker.

    Following the documentation on https://docs.docker.com/engine/userguide/containers/dockervolumes/

    If you are using Docker Machine on Mac or Windows, your Docker daemon has only limited access to your OS X or Windows filesystem. Docker Machine tries to auto-share your /Users (OS X) or C:\Users (Windows) directory. So, you can mount files or directories on OS X using.

    On Windows, mount directories using:

    docker run -v /c/Users/[path]:/[container path] ...`

    I tried:

    docker run --name iisdemo2 -it -p 80:80 -v /c/Users/mlin/meinedaten:/meinedaten iis cmd
    

    which gives me an error:

    docker : docker: Error response from daemon: Invalid bind mount spec "/c/Users/mlin/meinedaten:/meinedaten": volumeinvalid: Invalid volume specification: 
    '/c/Users/mlin/meinedaten:/meinedaten'.
    

    I also tried:

    docker run --name iisdemo2 -it -p 80:80 -v /c/Users/mlin/meinedaten:/c/meinedaten iis cmd
    

    Note that the path C:\meinedaten on the guest/container exist already, which is required according to the docker documentation.

    The command looks correct to me according to the documentation.

    enter image description here

    (Mounting volumes from Mac OS X host to Ubuntu docker container works fine, I am just having problems with Windows.)

    Update

    I also just tried to use Windows Containers natively (via Powershell), not using Docker. I follow the documentation on https://msdn.microsoft.com/en-us/virtualization/windowscontainers/quick_start/manage_powershell#create-a-shared-folder.

    Add-ContainerSharedFolder -ContainerName mysql2 -SourcePath C:\Users\mlin\meinedaten -DestinationPath C:\meinedaten
    

    But I am getting problems there are as well.

    enter image description here

    Eventually related topics:

  • Steve Coleman
    Steve Coleman almost 6 years
    This does not work if you created the folders with a copy or add command.
  • FantomX1
    FantomX1 over 4 years
    the host dir can be also relative "./" and guest internal os might be linux type of os thus regular linux path "/a/b/c/" like , in docker compose this way ` volumes: - ./:/var/www/html`
  • Mathias Conradt
    Mathias Conradt about 4 years
    > Are you running the docker CLI from a Mac machine and trying to run Windows containers? --> No, all Windows. I already found the solution as marked. Note it's 4 years ago, I don't work with Windows Containers anymore these days.