docker 官方關於數據卷的解釋
https://docs.docker.com/storage/volumes/
數據卷解決的問題
共享主機與容器,容器與容器之間的公用數據。在容器內對卷的修改會立即在主機上生效
使用環境
系統:windows10
命令控制台:windows 的powershell docker 不建議使用 powershell ISE
特點
- 與綁定安裝相比,卷更易於備份或遷移。
- 您可以使用Docker CLI命令或Docker API管理卷。
- 卷在Linux和Windows容器上均可工作。
- 可以在多個容器之間更安全地共享卷。
- 卷驅動程序使您可以將卷存儲在遠程主機或雲提供程序上,以加密卷內容或添加其他功能。
- 可以通過容器預先填充新卷的內容。
卷的聲明周期獨立於容器的聲明周期
使用
兩種方式可以掛載數據卷 -v(--volume) 或 --mount ,docker 官方推薦使用--mount
優點:更明確的語意,雖然冗長,但是更好理解。 使用key=value 的方式,明確每個字段的含義
綁定的主機文件目錄若不存在則--mount 不會自動創建,而-v 會自動創建出一個目錄
掛載一個數據卷的過程
列出docker 鏡像: docker iamges
綁定並運行docker:
docker run -d -it --name mymount --mount type=bind,source="${pwd}",target=/data mytest
此時windows 會彈出一個windows share it的確認框 點擊share it
若docker 容器中沒有 data 文件夾則創建一個文件夾
紅框標記的地方是沒有點擊shrae it 爆出來的錯誤
命令解析
-d : detached 以分離模式運行,即后台模式 dtached 默認等於true 若設置為detached=false 則等同於沒有過-d ,即前台模式運行
-it : i 是 打印信息 -t 分配一個偽tty(終端)
--name :給數據卷命名
type: 分為三種 bind ,volume, tmpfs
source:宿主主機的文件路徑:我這里是 F:\docker\mountdir
target:docker 容器內的文件路徑
mytest :某個鏡像
docker inspect mymount(這是數據卷名) :查看數據卷的信息
嘗試修改容器內的數據, 查看綁定的文件夾內容是否同步變化
可以看到在容器中創建一個文件立馬在主機映射的目錄下也創建一個文件
在主機上直接創建一個文件但是在容器內卻看不到,有待驗證是否有其他方式查看注解內容,不然怎么在容器之間做資源共享。
刪除容器內的文件並不能刪除主機上的文件這說明:數據卷主機的內容並不會隨着容器的數據銷毀而銷毀。
停止和刪除數據卷
刪除之后再在容器內創建文件並不會映射到主機的目錄下
其他特性
支持多個數據卷同時綁定
支持設置主機數據卷的讀寫權限
支持多種隔離級別
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. |
多種持久性方式