Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mount a Windows' folder in Docker using Powershell (or CMD)?

I would like to run a container on Windows 10 and mount my local folder to a folder in the container. Let's take the following command as an example, but any container will do.

 docker run -v "$(pwd)":/data -- name mongo -d mongo mongod --smallfiles

The issue is with the pwd command. In a Unix environment it returns a relative path, but in Windows it returns an absolute path.

I have tried replacing "$(pwd)" with "." or even "./" with no luck. I also tried ($pwd | Resolve-Path -Relative). In all cases I get

Error parsing reference: ":/data" is not a valid repository/tag.

What works is replacing "$(pwd)" with /d/path/to/my/folder.

docker run -v /d/path/to/my/folder:/data -- name mongo -d mongo mongod --smallfiles

(which is d:\path\to\my\folder) and the mounting is done correctly. However, I'd like to make the command generic, so it can be run from any folder on the host.

like image 769
Roman Mik Avatar asked Aug 24 '16 21:08

Roman Mik


People also ask

How do I mount a Windows host directory in a Docker container?

How to Mount Local Directories using docker run -v. Using the parameter -v allows you to bind a local directory. -v or --volume allows you to mount local directories and files to your container. For example, you can start a MySQL database and mount the data directory to store the actual data in your mounted directory.

How do I map a Windows Docker drive?

In order to share Windows folders with Docker containers, you first need to configure the "Shared Drives" option in Docker settings. Once the Shared Drives option is configured, you can mount any folder on shared drives with the "-v" (volume) flag.

Can I mount a file in Docker?

The Docker CLI provides the –mount and –volume options with a run command to bind a single file or directory. Both flags work similarly but have different syntaxes. As a result, we can use them interchangeably.


2 Answers

As documented /<drive>/<path> is the correct syntax for mounting folders:

On Windows, mount directories using:

docker run -v /c/Users/<path>:/<container path> ...

You can make your command more generic by transforming a path to what Docker expects:

$PWD.Path -replace '^|\\+','/' -replace ':'

like this:

docker run -v "$($PWD.Path -replace '^|\\+','/' -replace ':')":/data -- ...

If the drive letter must be lowercased (can't test, since I don't have Docker running on Windows) it gets a little more complicated. If you can lowercase the entire string you could replace $PWD.Path with $PWD.Path.ToLower(). If you must lowercase just the drive letter and preserve case in the rest of the path you could use a callback function:

$re = [regex]'^([A-Z]):'
$cb = { $args[0].Groups[1].Value.ToLower() }

docker run -v "$($re.Replace($PWD.Path, $cb) -replace '^|\\+','/')":/data -- ...
like image 188
Ansgar Wiechers Avatar answered Sep 19 '22 04:09

Ansgar Wiechers


This works in PowerShell:

docker run --rm -v ${PWD}:/data alpine ls /data

See Mount current directory as a volume in Docker on Windows 10.

like image 23
Johannes Buchholz Avatar answered Sep 19 '22 04:09

Johannes Buchholz