使用docker-compose绑定单个文件

Paf*_*low 2 containers 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
Run Code Online (Sandbox Code Playgroud)

例如可以在示例中找到。

每当我在./a/src目录中的主机或其中的容器中更改某些内容时,/var/www/html/它都会按预期在另一侧更改。它们与应有的相同。

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

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

注意:这与如何在卷中装载单个文件不是重复的。我的文件不是目录等文件。尽管如此,我还是尝试使用绝对目录,并按照建议进行了尝试,但这并没有改变。${PWD}

Docker version 19.03.1, build 74b1e89
docker-compose version 1.24.1, build 4667896b
Run Code Online (Sandbox Code Playgroud)

主机和容器系统是Debian。

mch*_*wre 6

请经历这个

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

如果使用像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.
Run Code Online (Sandbox Code Playgroud)

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

希望这可以帮助。

  • 如此简单、如此精确,但又如此难以找出。谢谢。 (3认同)