在 Windows 10 上的 Docker 中将当前目录挂载为卷
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41485217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mount current directory as a volume in Docker on Windows 10
提问by McGo
Description
描述
I am using Docker version 1.12.5 on Windows 10 via Hyper-V and want to use container executables as commands in the current path. I built a Docker image that is running fine, but I have a problem to mount the current path. The idea is to create an alias and do a docker run --rm [...]
command so that it could be used system-wide in the current directory.
我通过 Hyper-V 在 Windows 10 上使用 Docker 1.12.5 版,并希望将容器可执行文件用作当前路径中的命令。我构建了一个运行良好的 Docker 映像,但是我在挂载当前路径时遇到了问题。这个想法是创建一个别名并执行一个docker run --rm [...]
命令,以便它可以在当前目录中的系统范围内使用。
Setup
设置
I have a drive E with a folder "test" and in there a folder called "folder on windows host" to show that the command is working. The Dockerfile create the directory /data
, defines it as VOLUME and WORKDIR.
我有一个带有文件夹“test”的驱动器 E 和一个名为“windows主机上的文件夹”的文件夹,以显示该命令正在运行。Dockerfile 创建目录/data
,将其定义为 VOLUME 和 WORKDIR。
Having E:\test
as the current directory in PowerShell and executing the Docker command with an absolute path, I can see the content of E:\test
:
有E:\test
在PowerShell中的当前目录和绝对路径执行多克尔命令,我可以看到的内容E:\test
:
PS E:\test> docker run --rm -it -v E:\test:/data mirkohaaser/docker-clitools ls -la
total 0
drwxr-xr-x 2 root root 0 Jan 4 11:45 .
drwxr-xr-x 2 root root 0 Jan 5 12:17 folder on windows host
Problem
问题
I want to use the current directory and not an absolute notation. I could not use pwd in the volume because of different error messages:
我想使用当前目录而不是绝对符号。由于不同的错误消息,我无法在卷中使用 pwd:
Trying with ($pwd)
尝试使用 ($pwd)
PS E:\test> docker run --rm -it -v ($pwd):/data mirkohaaser/docker-clitools ls -la
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error parsing reference: ":/data" is not a valid repository/tag.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
Trying with /($pwd)
尝试 /($pwd)
PS E:\test> docker run --rm -it -v /($pwd):/data mirkohaaser/docker-clitools ls -la
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error parsing reference: "E:\test" is not a valid repository/tag.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
Trying with \′pwd\′
尝试使用 \'pwd\'
PS E:\test> docker run --rm -it -v ′$pwd′:/data mirkohaaser/docker-clitools ls -la
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Invalid bind mount spec "′E:\test′:/data": invalid mode: /data.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
Trying with `pwd`
尝试使用`pwd`
PS E:\test> docker run --rm -it -v `$pwd`:/data mirkohaaser/docker-clitools ls -la
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: create $pwd: "$pwd" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
What is the correct syntax of mounting the current directory as a volume in Docker on Windows 10?
在 Windows 10 上的 Docker 中将当前目录安装为卷的正确语法是什么?
回答by ETL
In Windows Command Line (cmd
), you can mount the current directory like so:
在 Windows 命令行 ( cmd
) 中,您可以像这样挂载当前目录:
docker run --rm -it -v %cd%:/usr/src/project gcc:4.9
In PowerShell, you use ${PWD}
, which gives you the current directory:
在 PowerShell 中,您使用${PWD}
,它为您提供当前目录:
docker run --rm -it -v ${PWD}:/usr/src/project gcc:4.9
On Linux:
在 Linux 上:
docker run --rm -it -v $(pwd):/usr/src/project gcc:4.9
Cross Platform
跨平台
The following options will work on both PowerShell and on Linux (at least Ubuntu):
以下选项适用于 PowerShell 和 Linux(至少是 Ubuntu):
docker run --rm -it -v ${PWD}:/usr/src/project gcc:4.9
docker run --rm -it -v $(pwd):/usr/src/project gcc:4.9
回答by friism
This works for me in PowerShell:
这在 PowerShell 中对我有用:
docker run --rm -v ${PWD}:/data alpine ls /data
回答by ToastedSoul
For Git Bash for Windows (in ConEmu), the following works for me (for Docker Windows containers):
对于 Windows 版 Git Bash(在 ConEmu 中),以下内容适用于我(对于 Docker Windows 容器):
docker run --rm -it -v `pwd -W`:c:/api microsoft/dotnet:2-runtime
Note the backticks/backquotesaround pwd -W
!
注意周围的反引号/反引号pwd -W
!
With all other variations of PWD I've tried I've received: "Error response from daemon: invalid volume specification: ..."
对于我尝试过的所有其他 PWD 变体,我收到了: "Error response from daemon: invalid volume specification: ..."
Update: The above was for Docker Windows containers, for Linux containers use:
更新:以上适用于 Docker Windows 容器,适用于 Linux 容器:
docker run --rm -it -v `pwd -W`:/api -p 8080:80 microsoft/aspnetcore:2
回答by Paulo Merson
- Open Settingson Docker Desktop (Docker for Windows).
- Select Shared Drives.
- Select the drive that you want to use inside your containers (e.g., C).
The command below should now work on PowerShell (command prompt does not support
${PWD}
):docker run --rm -v ${PWD}:/data alpine ls /data
- 在 Docker 桌面(Docker for Windows)上打开设置。
- 选择共享驱动器。
- 选择要在容器内使用的驱动器(例如,C)。
下面的命令现在应该可以在 PowerShell 上运行(命令提示符不支持
${PWD}
):docker run --rm -v ${PWD}:/data alpine ls /data
IMPORTANT: if/when you change your Windows domain password, the mount will stop working silently, that is, -v
will work but the container will notsee your host folders and files. Solution: go back to Settings, uncheck the shared drives, Apply, check them again, Apply, and enter the new password when prompted.
重要提示:如果/当您更改 Windows 域密码时,挂载将停止静默运行,即可以运行,-v
但容器将看不到您的主机文件夹和文件。解决方案:返回设置,取消选中共享驱动器,应用,再次检查它们,应用,并在出现提示时输入新密码。
回答by jseguillon
Here is mine which is compatible for both Win10 docker-ce & Win7 docker-toolbox. At las at the time I'm writing this :).
这是我的,它与 Win10 docker-ce 和 Win7 docker-toolbox 兼容。终于在我写这篇文章的时候了:)。
You can notice I prefer use /host_mnt/c instead of c:/ because I sometimes encountered trouble on docker-ce Win 10 with c:/
您会注意到我更喜欢使用 /host_mnt/c 而不是 c:/ 因为我有时会在使用 c:/ 的 docker-ce Win 10 上遇到问题
$WIN_PATH=Convert-Path .
#Convert for docker mount to be OK on Windows10 and Windows 7 Powershell
#Exact conversion is : remove the ":" symbol, replace all "\" by "/", remove last "/" and minor case only the disk letter
#Then for Windows10, add a /host_mnt/" at the begin of string => this way : c:\Users is translated to /host_mnt/c/Users
#For Windows7, add "//" => c:\Users is translated to //c/Users
$MOUNT_PATH=(($WIN_PATH -replace "\","/") -replace ":","").Trim("/")
[regex]$regex='^[a-zA-Z]/'
$MOUNT_PATH=$regex.Replace($MOUNT_PATH, {$args[0].Value.ToLower()})
#Win 10
if ([Environment]::OSVersion.Version -ge (new-object 'Version' 10,0)) {
$MOUNT_PATH="/host_mnt/$MOUNT_PATH"
}
elseif ([Environment]::OSVersion.Version -ge (new-object 'Version' 6,1)) {
$MOUNT_PATH="//$MOUNT_PATH"
}
docker run -it -v "${MOUNT_PATH}:/tmp/test" busybox ls /tmp/test
回答by Blue Clouds
This command should fix it.
这个命令应该修复它。
docker run --rm -it -v ${PWD}:c:\data
mirkohaaser/docker-clitools
docker run --rm -it -v ${PWD}:c:\data
mirkohaaser/docker-clitools
{PWD} is the host current folder. after the :
is the container folder.
If the mounting is correct then files will be listed in the director c:\data
in the container.
{PWD} 是主机当前文件夹。之后:
是容器文件夹。如果安装正确,则文件将列在c:\data
容器中的 Director中。
回答by Karl
You need to swap all the back slashes to forward slashes so change
您需要将所有反斜杠交换为正斜杠,以便更改
docker -v C:\my\folder:/mountlocation ...
docker -v C:\my\folder:/mountlocation ...
to
到
docker -v C:/my/folder:/mountlocation ...
docker -v C:/my/folder:/mountlocation ...
I normally call docker from a cmd script where I want the folder to mount to be relative to the script i'm calling so in that script I do this...
我通常从 cmd 脚本调用 docker,我希望安装的文件夹与我正在调用的脚本相关,因此在该脚本中我这样做...
SETLOCAL
REM capture the path to this file so we can call on relative scrips
REM without having to be in this dir to do it.
REM capture the path to MSYS_NO_PATHCONV=1 docker run --rm -it -v $(pwd):/usr/src/project gcc:4.9
ie this script
set mypath=%~dp0
REM strip last char
set PREFIXPATH=%mypath:~0,-1%
echo "PREFIXPATH=%PREFIXPATH%"
mkdir -p %PREFIXPATH%\my\folder\to\mount
REM swap \ for / in the path
REM because docker likes it that way in volume mounting
set PPATH=%PREFIXPATH:\=/%
echo "PPATH=%PPATH%"
REM pass all args to this script to the docker command line with %*
docker run --name mycontainername --rm -v %PPATH%/my/folder/to/mount:/some/mountpoint myimage %*
ENDLOCAL
回答by pootzko
Other solutions for Git Bash provided by others didn't work for me. Apparently there is currently a bug/limitation in Git for Windows. See thisand this.
其他人提供的 Git Bash 的其他解决方案对我不起作用。显然,Git for Windows 目前存在一个错误/限制。看到这个和这个。
I finally managed to get it working after finding this GitHub thread(which provides some additional solutions if you're interested, which might work for you, but didn't for me).
在找到这个 GitHub 线程后,我终于设法让它工作了(如果你有兴趣,它提供了一些额外的解决方案,这可能对你有用,但对我没有用)。
I ended up using the following syntax:
我最终使用了以下语法:
docker run --rm -v /c/Users/Christian/manager/bin:/app --workdir=/app php:7.2-cli php app.php
Note the MSYS_NO_PATHCONV=1
in front of the docker
command and $(pwd)
- round brackets, lower-case pwd, no quotes, no backslashes.
注意命令MSYS_NO_PATHCONV=1
前面的- 圆括号,小写密码,没有引号,没有反斜杠。docker
$(pwd)
Also, I'm using Linux containers on Windows if that matters..
另外,如果重要的话,我在 Windows 上使用 Linux 容器。
I tested this in the new Windows Terminal, ConEmu and GitBash, and all of them worked for me.
我在新的 Windows 终端、ConEmu 和 GitBash 中对此进行了测试,它们都对我有用。
回答by Александр Рыкованов
cd /c/Users/Christian/manager
docker run --rm -v ${PWD}:/app --workdir=/app php:7.2-cli php bin/app.php
Git bash
git bash
cd C:\Users\Christian\manager
echo ${PWD}
result:
echo ${PWD}
结果:
/c/Users/Christian/manager
/c/用户/基督徒/经理
cmdor PowerShell
cmd或PowerShell
##代码##echo ${PWD}
result:
echo ${PWD}
结果:
Path ---- C:\Users\Christian\manager
路径 ---- C:\Users\Christian\manager
as we see in cmdor PowerShell$ {PWD} will not work
正如我们在cmd或PowerShell 中看到的$ {PWD} 将不起作用