问一个 docker 数据卷和宿主机目录挂载相关的问题

15 天前
 lizon

测试环境用下面这个 docker-compose.yml 即可

services:
  ubuntu:
    image: ubuntu:22.04
    volumes:
      # - volumeetc:/etc
      - ./data/etc:/etc
    command: tail -f /dev/null

volumes:
  volumeetc:

我的问题是:

如果使用 数据卷 volumeetc 挂载到 容器 /etc 目录, 启动容器后, 这个数据卷里的数据是完整的 etc 目录内的数据;

如果使用宿主机目录比如 ./data/etc 挂载到容器内, 宿主机 ./data/etc 这个目录下的数据不完整, 只有 hostname, hosts, resolv.conf 这三个文件;

我在 linux, mac, windows 都测试过, 都是这个行为;

调整过宿主机目录相关权限, 尽量和容器内保持一致, 也不行

我的问题是 为什么会有这种差异?

我个人推测跟容器创建的初始化流程有关, 但是没找到什么文档

希望大佬指条路

658 次点击
所在节点    Docker
3 条回复
lizon
15 天前
https://docs.docker.com/engine/storage/volumes/
查到官方文档有相关特性介绍

Populate a volume using a container
If you start a container which creates a new volume, and the container has files or directories in the directory to be mounted such as /app/, Docker copies the directory's contents into the volume. The container then mounts and uses the volume, and other containers which use the volume also have access to the pre-populated content.

看来是个刻意设计的特性...


GPT 问了一圈又一圈没回答到要点, 还是得翻文档才行
julyclyde
14 天前
从你这个 command 的写法来看……你可能没掌握容器的正确用法

(我这个回答和 volume 无关)
julyclyde
14 天前
你自己找到的这个答案,没看出和你的提问有啥关系啊
你的提问不是缺了大部分文件吗?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1071477

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX