记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.
- 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
- 使用 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 文件夹实时更新的配置做个记录,希望能帮到大家.