在我的docker-compose(3.7)文件中,我有类似的东西
- ./a/src/:/var/www/html/
- ./a/config/local.php.ini:/usr/local/etc/php/conf.d/local.ini
例如可以在this示例中找到。
每当我在./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。
答案 0 :(得分:6)
请通过this。
我想可能是由于this原因引起的。
如果使用文本编辑器(如vim)编辑文件,则保存 文件,它不会直接保存文件,而是会创建一个新文件 并将其复制到位。这打破了基于 在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.
我也强烈建议您通过this链接,它具有更多信息。
希望这会有所帮助。