如何在Docker中将卷从容器挂载到主机? [英] How to mount volume from container to host in Docker?
问题描述
我对Docker中的整个数据量过程有疑问。基本上,这里有两个Dockerfile及其各自的运行命令:
Dockerfile 1-
#通过Debian
#
#传输版本2.92
FROM debian:testing
RUN apt-get更新\
&& apt-get -y install nano \
&& apt-get -y install transmission-daemon传输-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传输-d -p 9091:9091 -v C:路径至配置:/ transmission / config -v C:路径至手表:/传输/手表-v C:要下载的路径:/传输/下载传输
Dockerfile 2-
#Nginx over Debian
#
#版本1.10.3
from debian:测试
运行apt-get update \
&& apt-get -y install nano \
&& apt-get -y install nginx
EXPOSE 80443
CMD [ nginx, -g,守护进程关闭;]
命令2-
docker run --name nginx -d -p 80:80 -v C:\path\to\config:/ etc / nginx -v C:\path\to\html :/ var / www / html nginx
所以,奇怪的是,第一个dockerfile和命令可以正常工作如预期。 docker守护程序将目录从容器装载到主机的位置。因此,我可以根据需要编辑配置文件,它们将在重新启动后持久保存到容器中。
但是,对于第二个dockerfile并命令它似乎没有用。我知道,如果您去查阅Docker Volume文档,它说卷安装只打算从主机到容器单向进行,但是Transmission容器如何按预期工作,而Nginx容器却没有? / p>
P:S-我正在运行 Microsoft Windows 10 Pro Build 14393 作为主机,并运行 Version 17.03.0-ce-win1 (10300)频道:beta 作为我的Docker版本。
编辑-只是为了澄清。我正在尝试将文件从Nginx容器内部发送到主机。在这方面,第一个容器(传输)通过使用数据量来工作。但是,对于第二个容器(Nginx),它不想将已安装目录中的文件从容器内部复制到主机。
主机卷不会从容器>主机中复制数据。主机卷安装在容器/映像的顶部,因此可以有效地用主机上的容器替换容器中的容器。
标准或命名卷 将复制容器映像中的现有数据进入新卷。通过启动具有 VOLUME $ 的容器来创建这些卷c $ c>
命令在Dockerfile中或通过docker命令
docker run -v myvolume:/ var /无论myimage
默认情况下,这是存储在本地卷中的数据,本地位于Docker主机。就您而言,这是在运行Docker的VM而不是Windows主机上,因此您可能不容易访问。
您可能会误认为传输自动在空白目录中生成文件以进行复制?
如果您确实需要保留VM Host>容器映射,则可能必须手动复制数据:
docker create --name nginxcopy nginx
docker cp nginxcopy:/ etc / nginx C:\路径\配置
docker cp nginxcopy:/ var / www / html C:\路径\html
docker rm nginxcopy
然后您可以将填充的主机目录映射到容器中,它们将具有图像随附的默认数据。
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"]
Command 1 -
docker run --name transmission -d -p 9091:9091 -v C:\path\to\config:/transmission/config -v C:\path\to\watch:/transmission/watch -v C:\path\to\download:/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;"]
Command 2 -
docker run --name nginx -d -p 80:80 -v C:\path\to\config:/etc/nginx -v C:\path\to\html:/var/www/html nginx
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.
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 - 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.
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.
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
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?
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\to\config
docker cp nginxcopy:/var/www/html C:\path\to\html
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屋!