乐趣区

关于docker:记docker容器内volume-mount实时更新

记 docker 容器内 volume mount 实时更新

一. 查看容器配置

docker inspect [container id or name]
 "Mounts": [
            {
                "Type": "bind",
                "Source": "D:\\workspace\\dracohub",
                "Destination": "/app",
                "Mode": "","RW": true,"Propagation":"rprivate"
            },
            {
                "Type": "bind",
                "Source": "D:\\workspace\\dracohub\\deploy\\php.ini",
                "Destination": "/opt/bitnami/php/conf/php.ini",
                "Mode": "","RW": false,"Propagation":"rprivate"
            }
        ],

Propagation 的值为 rprivate , 文件夹不实时同步的起因就是因为这个, 官网文档有个表格, 我贴在这里

Propagation setting Description
shared Sub-mounts of the original mount are exposed to replica mounts, and sub-mounts of replica mounts are also propagated to the original mount.
slave similar to a shared mount, but only in one direction. If the original mount exposes a sub-mount, the replica mount can see it. However, if the replica mount exposes a sub-mount, the original mount cannot see it.
private The mount is private. Sub-mounts within it are not exposed to replica mounts, and sub-mounts of replica mounts are not exposed to the original mount.
rshared The same as shared, but the propagation also extends to and from mount points nested within any of the original or replica mount points.
rslave The same as slave, but the propagation also extends to and from mount points nested within any of the original or replica mount points.
rprivate The default. The same as private, meaning that no mount points anywhere within the original or replica mount points propagate in either direction.

我大体上翻译一下:

设置类型 Description
shared 任何挂载了此目录的容器都会双向和宿主机实时更新, 作用范畴当前目录, 不包含子目录
slave 任何挂载了此目录的容器都会收到实时更新, 更新是单向的; 宿主机到容器, 作用范畴当前目录, 不包含子目录
private 任何挂载了此目录的容器都不会收到实时更新
rshared 同 shared, 作用范畴也包含子目录
rslave 同 slave, 作用范畴也包含子目录
rprivate 同 private, 作用范畴也包含子目录

二. 解决办法

咱们本地开发的时候是想要文件可能同步实时更新的, 所以须要设置为 rslave.

  1. docker 启动时能够间接命令配置为 rslave
 docker run -d \
  -it \
  --name devtest \
  --mount type=bind,source="$(pwd)"/target,target=/app \
  --mount type=bind,source="$(pwd)"/target,target=/app2,readonly,bind-propagation=rslave \
  nginx:latest
  1. 应用 docker-compose up 时能够批改配置文件 docker-compose.yml
volumes:
      - type: bind
        source: ./
        target: /app
        read_only: false
        bind:
          propagation: rslave
      - type: bind
        source: ./deploy/php.ini
        target: /opt/bitnami/php/conf/php.ini
        read_only: true
        bind:
          propagation: rslave

volumes 不应用简写, 这些配置就能够, 配置好了再看一下容器属性

"Mounts": [
            {
                "Type": "bind",
                "Source": "D:\\workspace\\dracohub",
                "Destination": "/app",
                "Mode": "","RW": true,"Propagation":"rslave"
            },
            {
                "Type": "bind",
                "Source": "D:\\workspace\\dracohub\\deploy\\php.ini",
                "Destination": "/opt/bitnami/php/conf/php.ini",
                "Mode": "","RW": false,"Propagation":"rslave"
            }
        ],

这样的话文件就是实时更新的, 然而是单向的, 是从宿主机到容器, 想改成什么能够依据本人需要.

对这次的 docker 文件夹实时更新的配置做个记录, 心愿能帮到大家.

退出移动版