简体   繁体   English

如何在使用 Windows 主机时挂载 Docker 卷?

[英]How do I mount a Docker volume while using a Windows host?

Mounting a Docker volume while being under a Windows host, has been a huge pain for me, and I could not get it to work.在 Windows 主机下安装 Docker 卷对我来说是一个巨大的痛苦,我无法让它工作。

Currently I got the following simple Dockerfile:目前我得到了以下简单的 Dockerfile:

FROM php:5-apache
RUN apt-get update

When I build an image from it, and start a container当我从中构建图像并启动容器时

docker build -t phisch:dev .
docker run phisch:dev

the container starts properly.容器正常启动。

But when I am trying to mount a volume,但是当我试图挂载一个卷时,

docker run -v /c/Users/phisch/dev/htdocs:/var/www phisch:dev

the following message will be displayed:将显示以下消息:

C:\Users\phisch\dev>docker run -v /c/Users/phisch/dev/htdocs:/var/www phisch:dev
no such file or directory
docker: Error response from daemon: Container command not found or does not exist..

The /var/www directory definitely exists in the container, and trying other directories does not change the result. /var/www目录肯定存在于容器中,尝试其他目录并不会改变结果。 Prepending a trailing slash to the host-side directory does not help either.在主机端目录前添加一个斜杠也无济于事。 ( //c/Users/phisch/dev/htdocs ) ( //c/Users/phisch/dev/htdocs )

How do I mount for example /var/www to C:/Users/phisch/dev/htdocs ?例如,如何将/var/www挂载到C:/Users/phisch/dev/htdocs

phisch@DESKTOP-UC1LB9J MINGW64 ~/dev (master)
$ docker inspect phisch:dev
[
    {
        "Id": "sha256:73c1533222a905a378f12505ccbd9e9b34cde5a4b34ed008c39e23d5d58a9c91",
        "RepoTags": [
            "dev_web:latest",
            "phisch:dev",
            "phisch:dev3"
        ],
        "RepoDigests": [],
        "Parent": "sha256:d2c4149d86c4dfceaff0e9c4eb5a5d42ca7815f81dd08baad4dc8bda6db2fb10",
        "Comment": "",
        "Created": "2016-02-10T12:16:37.667236134Z",
        "Container": "dad811f51ef3b94d9845d13a0e43ad07ccd5684ea2747b3846accdc71abeb628",
        "ContainerConfig": {
            "Hostname": "e06f5a03fe1f",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "PHP_INI_DIR=/usr/local/etc/php",
                "PHP_EXTRA_BUILD_DEPS=apache2-dev",
                "PHP_EXTRA_CONFIGURE_ARGS=--with-apxs2",
                "GPG_KEYS=0BD78B5F97500D450838F95DFE857D9A90D90EC1 6E4F6AB321FDC07F2C332E3AC2BF0BC433CFC8B3",
                "PHP_VERSION=5.6.18",
                "PHP_FILENAME=php-5.6.18.tar.xz",
                "PHP_SHA256=54dd9106c3469bc7028644d72ac140af00655420bbaaf4a742a64e9ed02ec1b0"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "apt-get update"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:d2c4149d86c4dfceaff0e9c4eb5a5d42ca7815f81dd08baad4dc8bda6db2fb10",
            "Volumes": null,
            "WorkingDir": "/var/www/html",
            "Entrypoint": null,
            "OnBuild": [],
            "Labels": {}
        },
        "DockerVersion": "1.10.0",
        "Author": "",
        "Config": {
            "Hostname": "e06f5a03fe1f",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "PHP_INI_DIR=/usr/local/etc/php",
                "PHP_EXTRA_BUILD_DEPS=apache2-dev",
                "PHP_EXTRA_CONFIGURE_ARGS=--with-apxs2",
                "GPG_KEYS=0BD78B5F97500D450838F95DFE857D9A90D90EC1 6E4F6AB321FDC07F2C332E3AC2BF0BC433CFC8B3",
                "PHP_VERSION=5.6.18",
                "PHP_FILENAME=php-5.6.18.tar.xz",
                "PHP_SHA256=54dd9106c3469bc7028644d72ac140af00655420bbaaf4a742a64e9ed02ec1b0"
            ],
            "Cmd": [
                "apache2-foreground"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:d2c4149d86c4dfceaff0e9c4eb5a5d42ca7815f81dd08baad4dc8bda6db2fb10",
            "Volumes": null,
            "WorkingDir": "/var/www/html",
            "Entrypoint": null,
            "OnBuild": [],
            "Labels": {}
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 491287784,
        "VirtualSize": 491287784,
        "GraphDriver": {
            "Name": "aufs",
            "Data": null
        }
    }
]

It turns out the container-side directory /var/www needs to be empty, since it had a sub-directory www .事实证明容器端目录/var/www需要为空,因为它有一个子目录www Docker was not able to use it as a mounting point. Docker 无法将其用作安装点。

It is possible the / is interpreted as an option by the CMD Windows shell. /有可能被 CMD Windows shell 解释为一个选项。

Try first a docker-machine ssh default, in order to open an ssh session in your VM.首先尝试使用 docker-machine ssh 默认值,以便在您的 VM 中打开 ssh 会话。 From there try the docker run again: docker run -v /c/Users/phisch/dev/htdocs:/var/www phisch:dev那里再次尝试docker run -v /c/Users/phisch/dev/htdocs:/var/www phisch:dev

As commented by thaJeztah in issue 18290 :正如thaJeztahissue 18290 中评论的那样:

You could consider using docker-compose ;您可以考虑使用docker-compose docker-compose allows you to define bind-mounted volumes relative to the location of the docker-compose.yml file. docker-compose 允许您相对于docker-compose.yml文件的位置定义绑定安装的卷。
Using a docker-compose file allows you to specify all options needed to run your containers in a single file, which makes it ideal for sharing between team members (ie, just run docker-compose up -d will start all containers for the project with the right options).使用docker-compose文件允许您在单个文件中指定运行容器所需的所有选项,这使其成为团队成员之间共享的理想选择(即,只需运行docker-compose up -d将启动项目的所有容器正确的选择)。

This comment mentions a&dding a second /: 这条评论提到了一个&添加第二个/:

docker run -v //c/Users/phisch/dev/htdocs:`/var/www` phisch:dev

Even in the docker toolbox msys shell session, there are issues (like issue 282 )即使在 docker 工具箱 msys shell 会话中,也存在问题(如issue 282

Pyetro notes in the comments : Pyetro评论中指出:

In Windows, a double slash is needed at the beginning of the path to indicate the working directory.在 Windows 中,路径的开头需要一个双斜杠来指示工作目录。
Just to work with short path use like this:只是为了像这样使用短路径:

 docker run -v //$(PWD)/folder:/folder ...

After lengthy discussion, the issue was that /var/www had a folder in it.经过长时间的讨论,问题是/var/www里面有一个文件夹。

Mounting /c/Users/phisch/dev/htdoc onto an empty folder does work, but might not give the expected result, as the default CMD apache2-foreground might still serve its content based on /var/www (which would not have htdocs content if that htdocs is mounted onto another folder). /c/Users/phisch/dev/htdoc到一个空文件夹确实有效,但可能不会给出预期的结果,因为默认 CMD apache2-foreground可能仍基于/var/www提供其内容(不会有htdocs内容,如果该htdocs安装到另一个文件夹)。

I use Docker for Windows with PowerShell and use $PWD to refer to the current directory when mounting a volume, and it works well for me.我将 Docker for Windows 与 PowerShell 一起使用,并在挂载卷时使用$PWD来引用当前目录,这对我来说效果很好。 A couple of examples:几个例子:

docker run -p 2368:2368 -v $PWD/ghost:/var/lib/ghost -d ghost

docker run -p 2368:2368 -v $PWD/:/var/lib/ghost -d ghost

If we are talking about Docker on Windows then we have to take in account the fact that all containers are run on VirtualBox.如果我们谈论的是 Windows 上的 Docker,那么我们必须考虑到所有容器都在 VirtualBox 上运行的事实。

Before mounting volume to a container we have to be sure that particular folder is available for VirtualBox.在将卷安装到容器之前,我们必须确保特定文件夹可用于 VirtualBox。

Firstly, to define the name of the current running Docker machine, run首先,要定义当前运行的 Docker 机器的名称,运行

$ docker-machine.exe  active
default

Secondly, add shared folder to VirtualBox:其次,将共享文件夹添加到 VirtualBox:

$ VBoxManage sharedfolder add default --name "some_project" --hostpath D:\Projects\some_project

Thirdly, create the folder三、创建文件夹

$ docker-machine.exe ssh default 'sudo mkdir --parents /d/projects/some_project'

Fourthly, mount it:四、挂载:

$ docker-machine.exe ssh default 'sudo mount -t vboxsf some_project /d/projects/some_project'

Finally, create a container:最后,创建一个容器:

$ docker run -v //d/projects/some_project://d/projects/some_project -d some-image_name

First, make sure you run from powershell as a administrator.首先,确保您以管理员身份从powershell运行。

next, run a command like:接下来,运行如下命令:

docker run --name=ken -p 8080:80 -v //c/users/Ken/Desktop/docker:/usr/share/nginx/html:ro -d nginx

tested working on Windows 11.测试在 Windows 11 上工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM