首页 > 解决方案 > 使用 docker-compose 绑定挂载单个文件

问题描述

在我的 docker-compose (3.7) 文件中,我有类似的东西

  - ./a/src/:/var/www/html/
  - ./a/config/local.php.ini:/usr/local/etc/php/conf.d/local.ini

例如在这个例子中可以找到。

每当我在./a/src目录或容器中的主机上更改某些内容时,/var/www/html/它都会按预期在另一侧进行更改。他们应该是一样的。

文件不是这样。它被复制(我猜)到容器中。但是,如果我local.php.ini在主机上更改或/usr/local/etc/php/conf.d/local.ini另一个保持不变。

这是预期的行为吗?如果是,为什么以及是否可以更改它,这两个文件与目录相同

注意:这不是How to mount a single file in a volume的副本。我将文件作为文件而不是目录等。尽管如此,我还是按照那里的建议尝试了绝对目录,${PWD}但这没有任何改变。

Docker version 19.03.1, build 74b1e89
docker-compose version 1.24.1, build 4667896b

主机和容器系统是 Debian。

标签: dockerdocker-composecontainers

解决方案


请通过这个

我想可能是因为这个原因造成的。

如果您使用 vim 等文本编辑器编辑文件,当您保存文件时,它不会直接保存文件,而是创建一个新文件并将其复制到位。这会破坏基于 inode 的绑定挂载。由于保存文件有效地更改了 inode,因此更改不会传播到容器中。重新启动容器将获取新的 inode 并反映更改。

这是一个例子,解释我的意思:

# Create a file on host and list it contents and its inode number
-------------------
$ echo 'abc' > /root/file.txt
$ cat /root/file.txt 
abc
$ ls -ltrhi /root/
total 4K     
1623230 -rw-r--r--    1 root     root           4 Aug 23 17:44 file.txt
$
# Run an alpine container by mounting this file.txt
---------------------
$ docker run -itd -v /root/file.txt:/var/tmp/file.txt alpine sh
d59a2ad308d2de7dfbcf042439b295b27370e4014be94bc339f1c5c880bf205f
$
# Check file contents of file.txt and its inode number inside alpine container
$ docker exec -it d59a2ad308d2 sh
/ # cat /var/tmp/file.txt 
abc
/ # ls -ltrhi /var/tmp/
total 4K     
1623230 -rw-r--r--    1 root     root           4 Aug 23 17:44 file.txt
/ #

## NOTE: The inode number of file.txt is same here 1623230 on host and inside the container.

# Edit the file.txt inside alpine container using some text editor like vi
--------------------------
/ # vi /var/tmp/file.txt 
/ # ls -ltrhi /var/tmp/
total 4K     
1623230 -rw-r--r--    1 root     root           5 Aug 23 17:46 file.txt
/ # cat /var/tmp/file.txt 
abcd
/ #

# Check content of file.txt on host, it will be the same as the one inside container since the inode number of file.txt inside container and on host is still same 1623230 
--------------------------
$ cat /root/file.txt   <<=== ran it on host
abcd

# Now edit content of file.txt on host and check its inode number.
$ vi file.txt 
$ ls -ltrhi /root/
total 4K     
 862510 -rw-r--r--    1 root     root           6 Aug 23 17:47 file.txt
$ cat file.txt 
abcde
$ 

## NOTE: the inode number of file.txt on host is changed to 862510 after editing the file using vi editor.

# Check content of file.txt inside alpine container and list it inode number
----------------------------
$ docker exec -it d59a2ad308d2 sh
/ # ls -ltrhi /var/tmp/
total 4K     
1623230 -rw-r--r--    0 root     root           5 Aug 23 17:46 file.txt
/ # cat /var/tmp/file.txt 
abcd
/ #

## NOTE: inode number here is the old one and doesn't match with the one on the host and hence the content of file.txt also doesn't match.

# Restart alpine container
---------------------------
$ docker restart d59a2ad308d2
d59a2ad308d2
$ docker exec -it d59a2ad308d2 sh
/ # cat /var/tmp/file.txt 
abcde
/ # ls -ltrhi /var/tmp/
total 4K     
 862510 -rw-r--r--    1 root     root           6 Aug 23 17:47 file.txt
/ # [node1] (local) root@192.168.0.38 ~
$ 

## NOTE: After restarting container, the inode of file.txt is matching with the one on host and so the file contents also match.

我也强烈建议您通过链接,它有更多信息。

希望这可以帮助。


推荐阅读