为什么在挂载命名卷时docker-compose会创建一个空的主机文件夹? [英] Why does docker-compose create an empty host folder when mounting named volume?

查看:467
本文介绍了为什么在挂载命名卷时docker-compose会创建一个空的主机文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 docker-compose.yml 文件中包含以下内容:

I have the following in my docker-compose.yml file:

volumes:
    - .:/var/www/app
    - my_modules:/var/www/app/node_modules

之所以这样做是因为我的主机上没有 node_modules ,所有内容都已安装在映像中,并且位于 / var / www / app / node_modules

I do this because I don't have node_modules on my host and everything gets installed in the image and is located at /var/www/app/node_modules.

我对此有两个疑问:


  1. 一个空文件夹在名为 node_modules 的主机中创建。如果在 .yml 文件的列表中添加另一个卷(命名卷或匿名卷),该卷将显示在包含我的<$的同一文件夹中的主机目录中c $ c> .yml 文件。从答案来看,这似乎与以下事实有关:这两个映射同时进行。但是,为什么主机上的文件夹为空?

  1. An empty folder gets created in my host named node_modules. If I add another volume (named or anonymous) to the list in my .yml file, it'll show up in my host directory in the same folder that contains my .yml file. From this answer, it seems to have to do with the fact that there's these two mappings going on simultaneously. However, why is the folder empty on my host? Shouldn't it either a) contain the files from the named volume or b) not show up at all on the host?

Docker不应该检查吗?要么a)包含命名卷中的文件,要么b)根本不显示在主机上?初始化卷时,映像中的基础 / var / www / app / node_modules 而不是只说哦, node_modules 不存在(因为我假设主机绑定安装发生在命名卷初始化之前,因此 / var / www / app 应该不再有文件夹名为 node_modules 的文件,甚至在我在主机上创建一个示例 node_modules 文件夹并创建一个新卷时似乎也可以使用保留 my_modules:/ var / www / app / node_modules -它似乎仍然使用映像中的 node_modules 而不是

How does Docker know to check the underlying /var/www/app/node_modules from the image when initializing the volume rather than just saying "Oh, node_modules doesn't exist" (since I'm assuming the host bind mount happens before the named volume gets initialized, hence /var/www/app should no longer have a folder named node_modules. It seems like it even works when I create a sample node_modules folder on my host and a new volume while keeping my_modules:/var/www/app/node_modules—it seems to still use the node_modules from the image rather than from the host (which is not what I expected, although not unwanted).


推荐答案

作为实现细节,Docker在每次挂载时实际上都使用Linux内核文件系统挂载功能。一卷。要挂载卷,必须将其挂载到目录上,因此,如果挂载目标尚不存在,它将创建一个新的空目录作为挂载点。如果挂载点本身位于已挂载的卷中,则会看到创建了空目录,但挂载不会被回显。

As an implementation detail, Docker actually uses the Linux kernel filesystem mount facility whenever it mounts a volume. To mount a volume it has to be mounted on to a directory, so if the mount target doesn't already exist, it creates a new empty directory to be the mount point. If the mount point is itself inside a mounted volume, you'll see the empty directory get created, but the mount won't get echoed out.

(如果重新安装在Linux主机上,请在容器运行时尝试在外壳中运行 mount 。)

(If you're on a Linux host, try running mount in a shell while the container is running.)

即:


  • / container_root / app 是对的绑定安装/ host_path / app ;它们是相同的基础文件。

  • mkdir / container_root / app / node_modules 创建 / host_path / app /

  • / container_root / app / node_modules 上安装其他东西不会引起任何后果。可以安装在 / host_path / app / node_modules 上。

  • ...这将留下一个空的 / host_path / app / node_modules 目录。

  • /container_root/app is a bind mount to /host_path/app; they are they same underlying files.
  • mkdir /container_root/app/node_modules creates /host_path/app/node_modules too.
  • Mounting something else on /container_root/app/node_modules doesn't cause anything to be mounted on /host_path/app/node_modules.
  • ...which leaves an empty /host_path/app/node_modules directory.

首次启动容器,只有这样,如果您将空卷装入容器,图像中的内容被复制到卷中。您是在告诉Docker这个目录包含关键的 data ,它们需要保留的时间比容器的使用寿命更长。 不要使用主机目录卷旋钮不是魔术,并且,如果您执行诸如更改 package.json 文件之类的操作,Docker将不会更新其中的内容。这个数量。

The first time you start a container, and only then, if you mount an empty volume into a container, the contents from the image get copied into the volume. You're telling Docker this directory contains critical data that needs to be persisted for longer than the lifespan of the container. It is not a magic "don't use the host directory volume" knob, and if you do things like change your package.json file, Docker will not update the contents of this volume.

这篇关于为什么在挂载命名卷时docker-compose会创建一个空的主机文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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