使用 Windows 主机时如何挂载 Docker 卷? [英] How do I mount a Docker volume while using a Windows host?

查看:71
本文介绍了使用 Windows 主机时如何挂载 Docker 卷?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Windows 主机下安装 Docker 卷对我来说是一个巨大的痛苦,我无法让它工作.

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.

目前我得到了以下简单的 Dockerfile:

Currently I got the following simple 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

容器正常启动.

但是当我尝试挂载一个卷时,

But when I am trying to mount a volume,

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

将显示以下消息:

C:Usersphischdev>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..

/var/www 目录肯定存在于容器中,尝试其他目录不会改变结果.在主机端目录前加一个斜杠也无济于事.(//c/Users/phisch/dev/htdocs)

The /var/www directory definitely exists in the container, and trying other directories does not change the result. Prepending a trailing slash to the host-side directory does not help either. (//c/Users/phisch/dev/htdocs)

我如何将例如 /var/www 挂载到 C:/Users/phisch/dev/htdocs?

How do I mount for example /var/www to 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
        }
    }
]

事实证明容器端目录 /var/www 需要为空,因为它有一个子目录 www.Docker 无法将其用作安装点.

It turns out the container-side directory /var/www needs to be empty, since it had a sub-directory www. Docker was not able to use it as a mounting point.

推荐答案

/ 可能被 CMD Windows shell 解释为一个选项.

It is possible the / is interpreted as an option by the CMD Windows shell.

首先尝试使用默认的 docker-machine ssh,以便在您的 VM 中打开 ssh 会话.从那里再次尝试 docker run:docker run -v/c/Users/phisch/dev/htdocs:/var/www phisch:dev

Try first a docker-machine ssh default, in order to open an ssh session in your VM. From there try the docker run again: docker run -v /c/Users/phisch/dev/htdocs:/var/www phisch:dev

正如 thaJeztah问题18290:

您可以考虑使用docker-compose;docker-compose 允许您定义相对于 docker-compose.yml 文件位置的绑定安装卷.
使用 docker-compose 文件允许您在单个文件中指定运行容器所需的所有选项,这使其成为团队成员之间共享的理想选择(即,只需运行 docker-compose up-d 将使用正确的选项启动项目的所有容器).

You could consider using docker-compose; docker-compose allows you to define bind-mounted volumes relative to the location of the docker-compose.yml file.
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).

这条评论提到了一个&dding一秒钟/:

This comment mentions a&dding a second /:

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

即使在 docker 工具箱 msys shell 会话中,也存在问题(例如 issue 282)

Even in the docker toolbox msys shell session, there are issues (like issue 282)

经过长时间的讨论,问题是 /var/www 中有一个文件夹.

After lengthy discussion, the issue was that /var/www had a folder in it.

/c/Users/phisch/dev/htdoc 挂载到空文件夹中确实有效,但可能不会给出预期的结果,因为默认的 CMD apache2-foreground可能仍会基于 /var/www 提供其内容(如果 htdocs 安装到另一个文件夹中,则不会有 htdocs 内容).

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

这篇关于使用 Windows 主机时如何挂载 Docker 卷?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆