在Windows 10上的Docker中将当前目录作为卷挂载
说明
我正在通过Hyper-V在Windows 10上使用Docker 1.12.5版本,并希望在其中使用容器可执行文件作为命令当前路径。我构建了运行良好的Docker映像,但是挂载当前路径时遇到了问题。这个想法是创建一个别名并执行 docker run --rm [...]
命令,以便可以在当前目录的系统范围内使用它。
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.
设置
我有一个带 test文件夹的驱动器E一个名为 Windows主机上的文件夹的文件夹,以显示命令正在运行。 Dockerfile创建目录 / data
,将其定义为VOLUME和WORKDIR。
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.
具有 E:\test
作为PowerShell中的当前目录,并使用绝对路径执行Docker命令,我可以看到 E:\test $ c $的内容c>:
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
:
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
问题
我要使用当前目录,而不是绝对符号。由于不同的错误消息,我无法在卷中使用pwd:
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)
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'.
尝试使用/($ pwd)
Trying with /($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'.
尝试\´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'.
尝试`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'.
在Windows 10上的Docker中将当前目录作为卷挂载的正确语法是什么?
What is the correct syntax of mounting the current directory as a volume in Docker on Windows 10?
在Windows命令行( cmd
)中,可以挂载当前目录像这样:
In Windows Command Line (cmd
), you can mount the current directory like so:
docker run --rm -it -v %cd%:/usr/src/project gcc:4.9
在PowerShell中,您使用 $ {PWD}
,这会为您提供当前目录:
In PowerShell, you use ${PWD}
, which gives you the current directory:
docker run --rm -it -v ${PWD}:/usr/src/project gcc:4.9
在Linux上:
docker run --rm -it -v $(pwd):/usr/src/project gcc:4.9
跨平台
以下选项在PowerShell和Linux上均适用(至少Ubuntu):
The following options will work on both PowerShell and on Linux (at least 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