如何将卷从容器挂载到 Docker 中的主机? [英] How to mount volume from container to host in Docker?

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

问题描述

我有一个关于 Docker 中整个数据量过程的问题.基本上这里有两个 Dockerfile 和它们各自的运行命令:

I have a question regarding the whole data volume process in Docker. Basically here are two Dockerfiles and their respective run commands:

Dockerfile 1 -

# Transmission over Debian
#
# Version 2.92

FROM debian:testing

RUN apt-get update 
    && apt-get -y install nano 
    && apt-get -y install transmission-daemon transmission-common transmission-cli 
    && mkdir -p /transmission/config /transmission/watch /transmission/download

ENTRYPOINT ["transmission-daemon", "--foreground"]
CMD ["--config-dir", "/transmission/config", "--watch-dir", "/transmission/watch", "--download-dir", "/transmission/download", "--allowed", "*", "--no-blocklist", "--no-auth", "--no-dht", "--no-lpd", "--encryption-preferred"]

命令 1 -

docker run --name transmission -d -p 9091:9091 -v C:path	oconfig:/transmission/config -v C:path	owatch:/transmission/watch -v C:path	odownload:/transmission/download transmission  

Dockerfile 2 -

# Nginx over Debian
#
# Version 1.10.3

FROM debian:testing

RUN apt-get update 
    && apt-get -y install nano 
    && apt-get -y install nginx

EXPOSE 80 443

CMD ["nginx", "-g", "daemon off;"]

命令 2 -

docker run --name nginx -d -p 80:80 -v C:path	oconfig:/etc/nginx -v C:path	ohtml:/var/www/html nginx

所以,奇怪的是第一个 dockerfile 和命令按预期工作.docker 守护进程将目录从容器挂载到主机的位置.因此,我可以随意编辑配置文件,并且它们将在重新启动时持久保存到容器中.

So, the weird thing is that the first dockerfile and command works as intended. Where the docker daemon mounts a directory from the container to the host. So, I am able to edit the configuration files as I please and they will be persisted to the container on a restart.

但是,对于第二个 dockerfile 和命令,它似乎不起作用.我知道如果您查看 Docker Volume 文档,它会说卷挂载只是单向的,从主机到容器,但是传输容器如何按预期工作,而 Nginx 容器却没有?

However, as for the second dockerfile and command it doesn't seem to be working. I know if you go to the Docker Volume documentation it says that volume mounts are only intended to go one-way, from host-to-container, but how come the Transmission container works as intended, while the Nginx container doesn't?

P:S - 我正在运行 Microsoft Windows 10 Pro Build 14393 作为我的主机和 版本 17.03.0-ce-win1 (10300) 频道:测试版我的 Docker 版本.

P:S - I'm running Microsoft Windows 10 Pro Build 14393 as my host and Version 17.03.0-ce-win1 (10300) Channel: beta as my Docker version.

编辑 - 只是为了澄清.我正在尝试将文件从 Nginx 容器内部获取到主机.第一个容器(传输)通过使用数据卷在这方面起作用.但是,对于第二个容器(Nginx),它不想将挂载目录中的文件从容器内部复制到主机.其他一切正常,但它确实成功启动.

Edit - Just to clarify. I'm trying to get the files from inside the Nginx container to the host. The first container (Transmission) works in that regard, by using a data volume. However, for the second container (Nginx), it doesn't want to copy the files in the mounted directory from inside the container to the host. Everything else is working though, it does successfully start.

推荐答案

主机卷 不从容器 > 主机复制数据.主机卷挂载在容器/映像中内容的顶部,因此它们有效地将容器中的内容替换为主机上的内容.

Host volumes don't copy data from the container > host. Host volumes mount over the top of what's in the container/image, so they effectively replace what's in the container with what's on the host.

标准或命名"卷将容器映像中的现有数据复制到新卷中.这些卷是通过使用 VOLUME Dockerfile 中的命令或通过 docker 命令

A standard or "named" volume will copy the existing data from the container image into a new volume. These volumes are created by launching a container with the VOLUME command in it's Dockerfile or by the docker command

docker run -v myvolume:/var/whatever myimage

默认情况下,这是存储在本地"卷中的数据,本地"位于 Docker 主机上.在您的情况下,它位于运行 Docker 的 VM 上,而不是您的 Windows 主机上,因此您可能无法轻松访问.

By default this is data stored in a "local" volume, "local" being on the Docker host. In your case that is on the VM running Docker rather than your Windows host so might not be easily accessible to you.

您可能将空白目录中的传输自动生成文件误认为是副本?

You could be mistaking transmission auto generating files in a blank directory for a copy?

如果您确实需要保留 VM Host > 容器映射,那么您可能需要手动复制数据:

If you really need the keep the VM Host > container mappings then you might have to copy the data manually:

docker create --name nginxcopy nginx
docker cp nginxcopy:/etc/nginx C:path	oconfig
docker cp nginxcopy:/var/www/html C:path	ohtml
docker rm nginxcopy

然后您可以将填充的主机目录映射到容器中,它们将具有图像附带的默认数据.

And then you can map the populated host directories into the container and they will have the default data the image came with.

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

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