Docker 数据卷和挂载到主机

Docker Data Volumes and Mounting to Host

我刚刚通读了 Docker 文档,并试图更好地理解 Docker 数据量,文档中的内容有点模糊。

据我理解,有两种方法可以挂载 Docker 卷:

但是为了让卷在容器重启甚至容器换出(旧容器被删除,新容器是created/started)中持续存在,卷是否必须安装到主机在这两种方法中?!? 换句话说,如果我没有通过 "Simple Mount method" 显式挂载到主机,那么实际挂载到的卷在哪里?这个位置如何在容器交换中幸存下来?

此外,在这两种情况下,我假设该卷仅在给定主机的本地,并且如果您在多个主机上有 Swarm 或集群 运行,则无法使用这些命令,因此不同主机上的容器 运行 可以访问这些卷,是吗? (我猜这就是数据量容器发挥作用的地方,对吧?)提前致谢!

wouldn't the volume have to be mounted to the host machine in both approaches?!?

是的,这两种方法都基于在整个容器生命周期中持续存在的主机目录。

使用这些命令获取更多信息:

docker volume ls
docker volume inspect <volume-id>
# and
docker inspect <container-id>

(approach 1) where is the volume actually being mounted to?

这样做:

docker inspect <container-id>

你的答案在"Source":

"Mounts": [
            {
                "Type": "volume",
                "Name": "96f5e6531480cc716cd030f3f60f8927a55728a52d55ad55589752c2b89f2001",
                "Source": "/var/lib/docker/volumes/96f5e6531480cc716cd030f3f60f8927a55728a52d55ad55589752c2b89f2001/_data",
                "Destination": "/data",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],

请注意,如果您在 OSX 或 Windows 中,则 Source 指的是运行 docker.

的 VM 内的本地目录

And how does this location survive container swapouts?

它们存活下来是因为它们基于主机的本地目录(它们是卷,因此它们存在)。

if you have a Swarm or cluster running on multiple hosts, there's no way to use these commands so that containers running on different hosts could access these volumes, yes?

关于简单的卷配置,您是正确的。这就是 docker 变得棘手的地方,持久性。您可以在主机文件系统级别实现共享目录,然后在容器中挂载为卷,这样您就可以跨集群的主机获得共享卷。

https://docs.docker.com/engine/tutorials/dockervolumes/#mount-a-shared-storage-volume-as-a-data-volume