V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lizon
V2EX  ›  Docker

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

  •  
  •   lizon · 15 天前 · 658 次点击

    测试环境用下面这个 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 都测试过, 都是这个行为;

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

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

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

    希望大佬指条路

    3 条回复    2024-09-10 14:19:48 +08:00
    lizon
        1
    lizon  
    OP
       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
        2
    julyclyde  
       14 天前
    从你这个 command 的写法来看……你可能没掌握容器的正确用法

    (我这个回答和 volume 无关)
    julyclyde
        3
    julyclyde  
       14 天前
    你自己找到的这个答案,没看出和你的提问有啥关系啊
    你的提问不是缺了大部分文件吗?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   969 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 20:31 · PVG 04:31 · LAX 13:31 · JFK 16:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.