在 Windows 10 上的 Docker 中将当前目录挂载为卷

在 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.

设置

我有一个驱动器 E,其中有一个文件夹test",其中有一个名为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.

在PowerShell中以E: est为当前目录,以绝对路径执行Docker命令,可以看到E: est的内容:

Having E: est as the current directory in PowerShell and executing the Docker command with an absolute path, I can see the content of E: est:

PS E:	est> docker run --rm -it -v E:	est:/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:	est> docker run --rm -it -v ($pwd):/data mirkohaaser/docker-clitools ls -la
C:Program FilesDockerDockerResourcesindocker.exe: Error parsing reference: ":/data" is not a valid repository/tag.
See 'C:Program FilesDockerDockerResourcesindocker.exe run --help'.

尝试使用/($pwd)

Trying with /($pwd)

PS E:	est> docker run --rm -it -v /($pwd):/data mirkohaaser/docker-clitools ls -la
C:Program FilesDockerDockerResourcesindocker.exe: Error parsing reference: "E:\test" is not a valid repository/tag.
See 'C:Program FilesDockerDockerResourcesindocker.exe run --help'.

尝试使用 ´pwd´

PS E:	est> docker run --rm -it -v ´$pwd´:/data mirkohaaser/docker-clitools ls -la
C:Program FilesDockerDockerResourcesindocker.exe: Error response from daemon: Invalid bind mount spec "´E:\test´:/data": invalid mode: /data.
See 'C:Program FilesDockerDockerResourcesindocker.exe run --help'.

尝试使用`pwd`

PS E:	est> docker run --rm -it -v `$pwd`:/data mirkohaaser/docker-clitools ls -la
C:Program FilesDockerDockerResourcesindocker.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 FilesDockerDockerResourcesindocker.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