docker - 使用 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。
解决方案
请通过这个。
我想可能是因为这个原因造成的。
如果您使用 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.
我也强烈建议您通过此链接,它有更多信息。
希望这可以帮助。
推荐阅读
- c# - C# UWP如何获取mapi命名空间
- ruby - 无法触发 Bigcommerce 网络挂钩
- memory-management - 如何在内核中保留物理内存(arm64)
- mysql - 当mysql中的时间相同时如何按desc排序
- python - Python Pandas .loc 一次更新 2 列
- c# - 推荐在 App.xaml.cs 中调用 UWP MessageDialog 的方法
- apache-nifi - INFO [NiFi Bootstrap Command Listener] org.apache.nifi.bootstrap.RunNiFi Apache NiFi 现在正在运行并在端口 25249 上侦听 Boo tstrap 请求
- javascript - 将字符串数组转换为一个数组JS
- javascript - 角度返回 URL 错误
- mongodb - MongoDB 查询,用于显示之前的日志结果