主机和容器内部的相同文件不同,它与原始文件不同步 [英] The same file on host and inside container is different, it is not in sync with its original

查看:49
本文介绍了主机和容器内部的相同文件不同,它与原始文件不同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个怪异的问题:我以RW模式将文件从主机装载到容器,然后在主机上编辑此文件,但在容器中未更新该文件.

I've faced a weird problem: I mount a file from a host to a container in RW mode, then I edit this file on the host but it's not get updated in the container.

这是 docker inspect< container> 的输出的摘录:

"Mounts": [
    ...
    {
        "Type": "bind",
        "Source": "/home/a_user/projects/drupal/d8-default/composer.json",
        "Destination": "/var/www/d8-default/composer.json",
        "Mode": "rw",
        "RW": true,
        "Propagation": ""
    },
    ...
]

您可能会看到,该文件以读写模式挂载为单独的挂载.现在,当我在主机上对其进行编辑时,它不会在容器中被更改.我通过登录容器并在容器内外进行简单的 cat composer.json 来看到这一点.

As you may see, the file is mounted as read-write mode as a separate mount. Now when I edit it on the host, it doesn't get changed in the container. I see this by logging into the container and doing simple cat composer.json there and outside of it.

我已经对随机文件执行了其他测试,这是我发现的:

I've performed additional tests with random files and here is what I discovered:

  1. 如果正在编辑的文件位于目录(以RW模式挂载)中,则更改将立即出现在其容器的副本中.

  1. If a file being edited is located in a directory (which is mounted in RW mode) then changes immediately appear in its container's copy.

如果直接挂载文件,则更改根本不会转移,容器似乎正在维护其自己的文件版本!

If a file is mounted directly then changes are not transferred at all, it seems like the container is maintaining its own version of the file!

实际上,这是非常不希望的行为,它破坏了我的工作流程,并且我还没有找到解决这种错位的解决方案.欢迎任何建议.

Indeed it is highly undesired behavior, it breaks my workflows and I haven't found a solution to overcome this disalignment yet. Any suggestions are welcome.

推荐答案

将单个文件挂载到容器中时,要挂载的是Linux文件系统上文件的索引节点.如果替换文件(许多编辑器都这样做),则inode会更改.如果您挂载整个目录,这通常不是问题,因为指向inode的目录指针已更新.但是,当仅装入单个文件时,在启动容器后写入文件将不会在容器的内部和外部之间反映出来.

When you mount an individual file into a container, what you are mounting is the inode of the file on the linux filesystem. If you replace the file (which many editors do), then the inode changes. That's not normally an issue if you mount an entire directory because the directory's pointer to the inode gets updated. But when only mounting a single file, writes to the file after starting the container will not be reflected between the inside and outside of the container.

有关更多详细信息,请参见以下问题: https://github.com/moby/moby/issues/6011

For more details, see this issue: https://github.com/moby/moby/issues/6011

如果您的目标是修改容器外部的文件,并让该文件修改不能在卷装入中的文件目录中的单个文件,那么建议您将容器更改为具有与另一个目录中文件的符号链接,您可以在其中装载整个目录而不是单个文件.看起来像:

If your goal is to modify a file outside of the container, and have that modify a single file in a directory of files that can't be in the volume mount, then I'd recommend changing the container to have a symlink to a file in a different directory where you can mount the entire directory instead of a single file. That would look like:

在您的Dockerfile中:

In your Dockerfile:

RUN mkdir -p /var/docker/composer \
 && ln -s /var/docker/composer/composer.json /var/www/d8-default/composer.json

然后您的卷挂载为:

docker run -v /home/a_user/projects/drupal/d8-default:/var/docker/composer ...

这篇关于主机和容器内部的相同文件不同,它与原始文件不同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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