挂载 docker 主机卷但用容器的内容覆盖 [英] Mount docker host volume but overwrite with container's contents

查看:23
本文介绍了挂载 docker 主机卷但用容器的内容覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有几篇文章对理解 Docker 的卷和数据管理非常有帮助.这两个尤其出色:

Several articles have been extremely helpful in understanding Docker's volume and data management. These two in particular are excellent:

但是,我不确定我正在寻找的内容是否被讨论过.这是我的理解:

However, I am not sure if what I am looking for is discussed. Here is my understanding:

  1. 当运行 docker run -v/host/something:/container/something 时,主机文件将覆盖(但不覆盖)指定位置的容器文件.容器将无法再访问该位置以前的文件,而只能访问该位置的主机文件.
  2. 在 Dockerfile 中定义卷时,其他容器可能会共享镜像/容器创建的内容.
  3. 主机也可以查看/修改 Dockerfile 卷,但必须在使用 docker inspect 发现真正的挂载点之后.(通常类似于 /var/lib/docker/vfs/dir/cde167197ccc3e138a14f1a4f7c....).但是,当 Docker 必须在 Virtualbox 虚拟机中运行时,这很麻烦.
  1. When running docker run -v /host/something:/container/something the host files will overlay (but not overwrite) the container files at the specified location. The container will no longer have access to the location's previous files, but instead only have access to the host files at that location.
  2. When defining a volume in Dockerfile, other containers may share the contents created by the image/container.
  3. The host may also view/modify a Dockerfile volume, but only after discovering the true mountpoint using docker inspect. (usually somewhere like /var/lib/docker/vfs/dir/cde167197ccc3e138a14f1a4f7c....). However, this is hairy when Docker has to run inside a Virtualbox VM.

我的问题很简单.如何反转覆盖,以便在挂载卷时,容器文件优先于我的主机文件?

My question is simple. How can I reverse the overlay so that when mounting a volume, the container files take precedence over my host files?

我想指定一个可以轻松访问容器文件系统的挂载点.但似乎没有人问这个问题.我知道我可以为此使用数据容器,或者我可以使用 docker inspect 来查找挂载点,但在这种情况下,这两种解决方案都不是一个好的解决方案.

I want to specify a mountpoint where I can easily access the container filesystem. But there just doesn't seem to be anyone asking this question. I understand I can use a data container for this, or I can use docker inspect to find the mountpoint, but neither solution is a good solution in this case.

推荐答案

docker 1.10+ 共享文件的方式是通过卷,如 docker volume create.
这意味着,您不需要专门用于数据卷的容器,您可以直接使用数据卷.

The docker 1.10+ way of sharing files would be through a volume, as in docker volume create.
That means, you don't need a container dedicated to a data volume, you can use a data volume directly.

这样,您可以在容器中共享和安装该卷,然后将其内容保存在该卷中.
这更符合容器的工作方式:从主机隔离内存、cpu 和文件系统:这就是为什么你不能挂载一个卷并让容器的文件优先于主机文件": 这将打破容器隔离并将其内容暴露给主机.

That way, you can share and mount that volume in a container which will then keep its content in said volume.
That is more in line with how a container is working: isolating memory, cpu and filesystem from the host: that is why you cannot "mount a volume and have the container's files take precedence over the host file": that would break that container isolation and expose to the host its content.

这篇关于挂载 docker 主机卷但用容器的内容覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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