【问题标题】:Bind-Mount a single File with docker-compose使用 docker-compose 绑定挂载单个文件
【发布时间】:2019-08-23 17:11:26
【问题描述】:

在我的 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。

【问题讨论】:

  • 在容器启动后(在编辑任何内容之前)立即尝试stating 两个文件(在主机上和容器中)。检查 inode 是否匹配。在任意一侧编辑文件,再次检查。

标签: docker docker-compose containers


【解决方案1】:

请转至this

我猜这可能是因为this原因造成的。

如果您使用 vim 等文本编辑器编辑文件,当您保存 file 它不直接保存文件,而是创建一个新文件 并将其复制到位。这打破了绑定挂载,它基于 在 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.

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

希望这会有所帮助。

【讨论】:

  • 如此简单如此精确却又如此难以发现。谢谢。
猜你喜欢
  • 2019-01-09
  • 1970-01-01
  • 2021-05-08
  • 2017-05-09
  • 2019-04-29
  • 1970-01-01
  • 2016-05-31
  • 2019-02-26
  • 1970-01-01
相关资源
最近更新 更多