What's the correct way to mount a volume on Docker for Windows?

I’ve looked at a few similar issues and bugs online, but so far none of them have helped. What’s the correct way to mount a volume in a container running on Docker for Windows? I’m running Docker 18.06.1-ce on Windows 10 (Latest). First, I create a local directory called C:\DeleteMe, and did a docker pull hello-world for testing. I would like to mount the contents of DeleteMe in a directory on the container called Data.

I first tried:

docker run -it -v C:\DeleteMe:/Data hello-world cmd

But I got back:

C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error
response from daemon: invalid volume specification:
‘C:\DeleteMe:/Data’. See ‘C:\Program
Files\Docker\Docker\Resources\bin\docker.exe run --help’.

I also tried:

-v C:\DeleteMe:/Data hello-world cmd
-v C:\DeleteMe:/c/Data hello-world cmd
-v C:\DeleteMe:/c:/Data hello-world cmd
-v C:\DeleteMe:/c:\Data hello-world cmd

And about every other variety I could think of with various front slashes and back slashes for each. All have the same error message.

I’ve also tried the --mount syntax:

docker run -it --mount source=C:\DeleteMe,target=/Data hello-world cmd

And got a similar error:

C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error
response from daemon: invalid mount config for type “volume”: invalid
mount path: ‘/Data’. See ‘C:\Program
Files\Docker\Docker\Resources\bin\docker.exe run --help’.

What’s the secret?

I am using:

-v "C:\Docker\mysql-server:C:\db"

for my mssql container. Where C:\Docker\mssql-server is my local dir. C:\db will be automatically created in my container. / is used in linux only

Here are a few things that should have been more obvious in the documentation (or wished I knew)

The left part is the source path on the host, the right part is the path on the target/guest/container.

  • -v host:container

The source path is OS specific, on a Windows Host

  • -v foo:bar → if not path specified, defaults to the docker’s configured volume folder: ex: C:\ProgramData\Docker\volumes\in
  • -v “c:\foo:bar” → c:\foo (the double quotes are important), and must be fully qualified
  • -v “$(pwd)\foo:bar” → (the double quotes are important), and uses your current working folder, ex: c:\folder-where-you-run-the-command\foo

The target path is Container OS specific (Windows Host)

  • -v foo:bar → error
  • -v foo:c:\bar → in the container, c:\bar

Note: with -v, if the folder on the target does not exist, docker will create it.

1 Like

Thanks for sharing what you learned! :slight_smile: Just a small addition to the following line, because you didn’t mention it:

It would work only in PowerShell (or on Linux) which I like to use, but I don’t know how popular that is among beginner Docker users

HA ! good point… It just happens that PowerShell is my default “command prompt” (I’m a Windows Terminal converted ;))

  • Command Prompt, the syntax is %cd%\foo:/bar
  • PowerShell, the syntax can be any of:
    • $pwd\foo:/bar
    • ${pwd}\foo:/bar
    • “$pwd\foo:/bar”
    • “${pwd}\foo:/bar”
1 Like