docker07-數據存儲
- 2021 年 2 月 17 日
- 筆記
- docker, docker從入門到實踐
Docker 內部以及容器之間管理數據,在容器中管理數據主要有兩種方式:
-
-
-
-
-
-
數據卷(Volumes)
-
掛載主機目錄 (Bind mounts)
數據卷
是一個可供一個或多個容器使用的特殊目錄,它繞過 UFS,可以提供很多有用的特性:-
數據卷
可以在容器之間共享和重用 -
對
數據卷
的修改會立馬生效 -
對
數據卷
的更新,不會影響鏡像 -
數據卷
默認會一直存在,即使容器被刪除
注意:
數據卷
的使用,類似於 Linux 下對目錄或文件進行 mount,鏡像中的被指定為掛載點的目錄中的文件會複製到數據卷中(僅數據卷為空時會複製)。創建一個數據卷Volumes
#創建
[root@node1 ~]# docker volume create my-vol my-vol
#查看 [root@node1 ~]# docker volume ls DRIVER VOLUME NAME local my-vol
#詳細查看 [root@node1 ~]# docker volume inspect my-vol [ { "CreatedAt": "2021-02-17T10:54:32+08:00", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/my-vol/_data", "Name": "my-vol", "Options": {}, "Scope": "local" } ]啟動一個掛載數據卷的容器
[root@node1 ~]# docker run -itd --name web -v my-vol:/usr/share/nginx/html nginx:latest b87a20e4630fa1c1dda606a757ab27cd7d1708057610094a75ff4771dc8db6c0
查看web容器的詳細信息
[root@node1 ~]# docker inspect web ...... #找到mount字段 "Mounts": [ { "Type": "volume", "Name": "my-vol", "Source": "/var/lib/docker/volumes/my-vol/_data", "Destination": "/usr/share/nginx/html", "Driver": "local", "Mode": "z", "RW": true, "Propagation": "" } ],
刪除數據卷
[root@node1 ~]# docker volume rm my-vol
數據卷
是被設計用來持久化數據的,它的生命周期獨立於容器,Docker 不會在容器被刪除後自動刪除數據卷
,並且也不存在垃圾回收這樣的機制來處理沒有任何容器引用的數據卷
。如果需要在刪除容器的同時移除數據卷。可以在刪除容器的時候使用docker rm -v
這個命令。無主的數據卷可能會佔據很多空間,要清理請使用以下命令
$ docker volume prune
掛載一個主機目錄作為數據卷Bind mounts
使用 一個本地主機的目錄掛載到容器中去。
[root@node1 ~]# docker run -itd -P -v /html:/usr/share/nginx/html nginx:latest 1e691d2c4a72ef328d134e3448a9f59f5abf9150dfc7c08d8c15423922e32aa0
上面的命令加載主機的
/html
目錄到容器的/usr/share/nginx/html
目錄。這個功能在進行測試的時候十分方便,比如用戶可以放置一些程序到本地目錄中,來查看容器是否正常工作。本地目錄的路徑必須是絕對路徑,以前使用-v
參數時如果本地目錄不存在 Docker 會自動為你創建一個文件夾,現在使用--mount
參數時如果本地目錄不存在,Docker 會報錯。Docker 掛載主機目錄的默認權限是
讀寫
,用戶也可以通過增加readonly
指定為只讀
。[root@node1 ~]# docker run -itd -P -v /html:/usr/share/nginx/html:ro nginx:latest d646bf5df9f33ca50c7ab5e36ae59ca9a84cc13980a60162184806adc255eec2 #在宿主機目錄創建文件沒有問題 [root@node1 ~]# touch /html/index.html [root@node1 ~]# docker exec -it d646bf sh # cd /usr/share/nginx/html # touch index.html touch: cannot touch 'index.html': Read-only file system 觸摸:不能觸摸索引。只讀文件系統
查看數據卷的具體信息
[root@node1 ~]# docker inspect d646bf "Mounts": [ { "Type": "bind", "Source": "/html", "Destination": "/usr/share/nginx/html", "Mode": "ro", "RW": false, "Propagation": "rprivate" } ]
容器的跨主機網絡共享
通過nfsserver實現,管理2台nginx容器
IP 服務 192.168.1.1 nginx容器w1
192.168.1.2 nginx容器w2 192.168.1.4 nfs-server 1、搭建nfs
yum -y install nfs-utils mkdir /html vim /etc/exports cat /etc/exports #/html *(rw,sync,no_root_squash) systemctl start rpcbind systemctl enable rpcbind systemctl start nfs-server systemctl enable nfs-server
echo hello > /html/index.html2、docker01
[root@node1 ~]# mkdir /www [root@node1 ~]# showmount -e 192.168.1.4 Export list for 192.168.1.4: /html * [root@node1 ~]# mount -t nfs 192.168.1.4:/html /www
root@node1 ~]# ls /www/
index.html[root@node1 ~]# cat /www/index.html
hello[root@node1 ~]# docker run -itd –name w1 -p 666:80 -v /www:/usr/share/nginx/html nginx:latest
1c247e5147486ecd6f25feb623de727fdc1c54ddcf452e1bdd99f772381546d5訪問測試:
[root@node1 ~]# curl 192.168.1.1:666
hello3、docker02
[root@node2 ~]# mkdir /www [root@node2 ~]# showmount -e 192.168.1.4 Export list for 192.168.1.4: /html * [root@node2 ~]# mount -t nfs 192.168.1.4:/html /www [root@node2 ~]# cat /www/index.html hello [root@node2 ~]# docker run -itd --name w2 -p 666:80 -v /www:/usr/share/nginx/html nginx:latest 88fcd09b2ce93ef9085466a0ffb3f69fac66272bc0b4b39dec6a7886aa033f83
訪問測試 [root@node2 ~]# curl 192.168.1.2:666 hello4、修改nfs掛載文件,實現nginx容器網頁同步
[root@nfs-server html]# cat index.html hello [root@nfs-server html]# echo 404 > index.html [root@nfs-server html]# cat index.html 404 ————————————————————————————————————- [root@node1 ~]# curl 192.168.1.1:666 404 [root@node2 ~]# curl 192.168.1.2:666 404
-
-
-
-
-
-