Windows环境下Docker挂载卷时Linux格式路径被追加;C的问题求助
$PWD in Git Bash/PowerShell on Windows This is a super common gotcha with Git Bash on Windows, thanks to its over-helpful automatic path conversion behavior. Let's break down why this happens and how to fix it quickly.
Why the Error Happens
Git Bash tries to translate Unix-style paths (like /d/my/project/folder) to Windows-style paths (D:\my\project\folder) when passing them to Windows programs (like Docker Desktop). But when you append :/wkDir to $PWD, Git Bash misinterprets the colon as a separator for Windows drive letters. It then grabs the first drive letter from your PATH environment variable (usually C:) and tacks it on, turning your valid path into the broken D:/my/project/folder;C you're seeing.
Solutions
1. Disable Git Bash Path Conversion (Simplest Fix)
Add the MSYS_NO_PATHCONV=1 environment variable before your Docker command to turn off automatic path conversion entirely. This tells Git Bash to pass the Unix-style path as-is to Docker.
Update the Docker line in your .sh script to:
MSYS_NO_PATHCONV=1 docker run -v "$PWD:/wkDir" your-dev-image-name
You can also set this variable globally for your Git Bash session by adding export MSYS_NO_PATHCONV=1 at the top of your script.
2. Convert Path to Windows Format Explicitly
Use Git Bash's built-in cygpath tool to convert the Unix-style $PWD to a proper Windows path (with backslashes), which Docker understands perfectly.
Modify your script line to:
docker run -v "$(cygpath -w $PWD):/wkDir" your-dev-image-name
The -w flag tells cygpath to output a Windows-style path (e.g., D:\my\project\folder instead of /d/my/project/folder).
3. PowerShell-Specific Fix
If you're running the script through PowerShell (which spawns Git Bash), you can skip Git Bash's path conversion entirely by using PowerShell's native $PWD variable. PowerShell's $PWD.Path returns a Windows-style path directly, so you can use it in your command:
docker run -v "$($PWD.Path):/wkDir" your-dev-image-name
Alternatively, set the MSYS_NO_PATHCONV environment variable in PowerShell before running the script:
$env:MSYS_NO_PATHCONV=1; .\your-script.sh
Verify the Fix
Before running the full Docker command, add a test line to your script to check what path is being passed:
echo "Mount path: $PWD:/wkDir"
If you see something like D:/my/project/folder;C:/..., the path conversion is still active. Once you apply the fix, you should see the correct Unix-style path /d/my/project/folder:/wkDir (or the clean Windows-style path if using cygpath).
内容的提问来源于stack exchange,提问作者Sebastian Nemeth

