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 ~]# cat /www/index.html
root@node1 ~]# ls /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
-
-
-
-
-
-
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/260504.html
標籤:其他
下一篇:單鏈表之環形鏈表
