windows docker 資料卷(四)
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. |
多種永續性方式