Docker 數據管理Named volume詳解
Docker數據管理:Named volume
Docker中可以使用Named volume和data container來進行數據的管理。
單一Container的使用Helloworld
Step 1:創(chuàng)建一個Named Volume
事前確認volume的信息,沒有VOLUME存在
[root@host88 volumes]# docker volume ls DRIVER VOLUME NAME [root@host88 volumes]#
確認/var/lib/docker/volumes的狀況
[root@host88 volumes]# pwd /var/lib/docker/volumes [root@host88 volumes]# ll total 0 [root@host88 volumes]#
創(chuàng)建一個名為volname的數據卷,通過-v參數可以進行創(chuàng)建,同時也可以通過docker volume create來創(chuàng)建。
[root@host88 volumes]# docker run -it -v volname:/volumedata/dbdata debian root@b2e3523a6dd9:/# cd volumedata/dbdata root@b2e3523a6dd9:/volumedata/dbdata# ls -l total 0 root@b2e3523a6dd9:/volumedata/dbdata#
在Container外部確認此事volname是否已經創(chuàng)建成功
[root@host88 volumes]# docker volume ls DRIVER VOLUME NAME local volname [root@host88 volumes]#
確認/var/lib/docker/volumes下面 的情況
[root@host88 volumes]# pwd /var/lib/docker/volumes [root@host88 volumes]# ll total 0 drwxr-xr-x 3 root root 18 Jul 25 06:23 volname [root@host88 volumes]# find . -type f [root@host88 volumes]# find . -type d . ./volname ./volname/_data [root@host88 volumes]#
除了目錄結構沒有任何文件存在
Step 2:在Container中保存數據Hello world
root@b2e3523a6dd9:/volumedata/dbdata# ls -l total 0 root@b2e3523a6dd9:/volumedata/dbdata# echo "hello, world" >>helloworld root@b2e3523a6dd9:/volumedata/dbdata# cat helloworld hello, world root@b2e3523a6dd9:/volumedata/dbdata# ls -l total 4 -rw-r--r-- 1 root root 13 Jul 25 06:26 helloworld root@b2e3523a6dd9:/volumedata/dbdata#
在外部確認該信息是否已經存在
[root@host88 volumes]# pwd /var/lib/docker/volumes [root@host88 volumes]# find . -type f ./volname/_data/helloworld [root@host88 volumes]# cat ./volname/_data/helloworld hello, world [root@host88 volumes]#
Step 3:在外部直接修改該文件
[root@host88 volumes]# pwd /var/lib/docker/volumes [root@host88 volumes]# find . -type f ./volname/_data/helloworld [root@host88 volumes]# cat ./volname/_data/helloworld hello, world [root@host88 volumes]# echo "hell, this is `hostname`" >>./volname/_data/helloworld [root@host88 volumes]# cat ./volname/_data/helloworld hello, world hell, this is host88 [root@host88 volumes]#
在內部確認信息
root@b2e3523a6dd9:/volumedata/dbdata# ls -l total 4 -rw-r--r-- 1 root root 34 Jul 25 06:29 helloworld root@b2e3523a6dd9:/volumedata/dbdata# cat helloworld hello, world hell, this is host88 root@b2e3523a6dd9:/volumedata/dbdata#
從Container中退出前再追加一條信息
root@b2e3523a6dd9:/volumedata/dbdata# echo "hello, I will exit from `hostname`" >>helloworld root@b2e3523a6dd9:/volumedata/dbdata# cat helloworld hello, world hell, this is host88 hello, I will exit from b2e3523a6dd9 root@b2e3523a6dd9:/volumedata/dbdata#
Step 4:退出Container后看數據是否仍然存在
root@b2e3523a6dd9:/volumedata/dbdata# exit exit [root@host88 volumes]# find . -type f ./volname/_data/helloworld [root@host88 volumes]# cat ./volname/_data/helloworld hello, world hell, this is host88 hello, I will exit from b2e3523a6dd9 [root@host88 volumes]#
數據仍然存在。使用docker volume ls可以看到剛剛volname的數據卷也依然存在。
[root@host88 volumes]# docker volume ls DRIVER VOLUME NAME local volname [root@host88 volumes]#
數據卷的管理
docker的volume的管理目前主要有下面4種:create/ls/inspect/rm
查詢(ls)
[root@host88 volumes]# docker volume ls DRIVER VOLUME NAME local volname [root@host88 volumes]#
正常的環(huán)境一定不會跑出這么清靜的結果。
inspect
[root@host88 volumes]# docker volume inspect volname [ { "Name": "volname", "Driver": "local", "Mountpoint": "/var/lib/docker/volumes/volname/_data" } ] [root@host88 volumes]#
其實這個信息可能會覺得非常眼熟,看完docker insepect 的結果就會發(fā)現,內容是一致的,以下是docker inspect b2e3523a6dd9的mounts相關信息
"Mounts": [ { "Name": "volname", "Source": "/var/lib/docker/volumes/volname/_data", "Destination": "/volumedata/dbdata", "Driver": "local", "Mode": "z", "RW": true, "Propagation": "rslave" } ],
刪除(rm)
刪除之前的確認
[root@host88 volumes]# pwd /var/lib/docker/volumes [root@host88 volumes]# find . -type f ./volname/_data/helloworld [root@host88 volumes]# cat ./volname/_data/helloworld hello, world hell, this is host88 hello, I will exit from b2e3523a6dd9 [root@host88 volumes]# docker volume ls DRIVER VOLUME NAME local volname [root@host88 volumes]#
刪除volume之前需要刪除與其有依賴關系的container
[root@host88 volumes]# docker rm b2e3523a6dd9 b2e3523a6dd9 [root@host88 volumes]#
刪除container并不會將volume一并刪除
[root@host88 volumes]# pwd /var/lib/docker/volumes [root@host88 volumes]# find . -type f ./volname/_data/helloworld [root@host88 volumes]#
而使用docker volume rm則會干凈地刪除掉所有信息
[root@host88 volumes]# docker volume rm volname volname [root@host88 volumes]# ll total 0 [root@host88 volumes]# docker volume ls DRIVER VOLUME NAME [root@host88 volumes]#
長時間運行的Docker環(huán)境中,成為僵尸的不只是/var/lib/docker/volumes下面的實際數據
而且docker volume ls中也會有很多完全不知道的信息,甚至有些相關聯的實際數據已經被刪除
這種情況在很多考慮不足的環(huán)境中屢見不鮮,雖然只是很簡單的helloworld
數據管理時候需要考慮的問題還是值得引起足夠的重視。
創(chuàng)建(create):
可以像例子那樣通過run 和-v創(chuàng)建volume,同時也可以使用docker volume create來創(chuàng)建
[root@host88 volumes]# docker volume create --driver=local --name=volname volname [root@host88 volumes]# docker volume ls DRIVER VOLUME NAME local volname [root@host88 volumes]#
有些volume在創(chuàng)建時還要結合使用–opt參數(或者-o)
如果不指定–name參數,docker會體貼地替你取一個,大概就像下面這樣
[root@host88 volumes]# docker volume ls DRIVER VOLUME NAME local volname [root@host88 volumes]# docker volume create e54a0022fdff1e0e57b8635317e0b51b1e36c3c9b8c48a051e7778a45f08a83d [root@host88 volumes]# docker volume ls DRIVER VOLUME NAME local volname local e54a0022fdff1e0e57b8635317e0b51b1e36c3c9b8c48a051e7778a45f08a83d [root@host88 volumes]#
看著太鬧心了,一下全部刪掉吧。
[root@host88 volumes]# docker volume rm $(docker volume ls -q) volname e54a0022fdff1e0e57b8635317e0b51b1e36c3c9b8c48a051e7778a45f08a83d [root@host88 volumes]#
需要注意的是這個名字必須是唯一的,所以前面也說到過不使用docker volume rm來刪除的話會導致問題,
下次用同樣名字想要創(chuàng)建一個volume卻發(fā)現已經存在的時候就只能是創(chuàng)建失敗了。
多Container共用一個數據卷
Step 1:創(chuàng)建一個Named Volume
用你喜歡的方式創(chuàng)建一個named volume
[root@host88 volumes]# docker volume create --name=volname volname [root@host88 volumes]# docker volume ls DRIVER VOLUME NAME local volname [root@host88 volumes]#
Step 2:路人甲Container與之相連
[root@host88 volumes]# docker run -it -v volname:/volumedata/dbdata debian root@5a43b6347b53:/#
路人甲使用Debian,他想知道誰是docker的主機
root@5a43b6347b53:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var volumedata root@5a43b6347b53:/# cd volumedata/dbdata root@5a43b6347b53:/volumedata/dbdata# ls -l total 0 root@5a43b6347b53:/volumedata/dbdata# echo "hello, world by `hostname`, who is host?" >> helloworld root@5a43b6347b53:/volumedata/dbdata# cat helloworld hello, world by 5a43b6347b53, who is host? root@5a43b6347b53:/volumedata/dbdata#
Step 3:主機與路人乙
主機此時看到了這個信息
[root@host88 volumes]# pwd /var/lib/docker/volumes [root@host88 volumes]# find . -type f ./volname/_data/helloworld [root@host88 volumes]# cat ./volname/_data/helloworld hello, world by 5a43b6347b53, who is host? [root@host88 volumes]#
同時路人乙也與該volume進行了連接
[root@host88 ~]# docker run -it -v volname:/volumedata/dbdata centos [root@6365668cea55 /]#
BTW,Docker現在不能使用相對路徑,所以volname:/volumedata/dbdata的這個寫法最前面的/仍然是不可或缺.
路人乙說雖然你不是找我,但是我看見了,這是共享的,我就可以回信么,說我不知道。
[root@6365668cea55 dbdata]# ls -l total 4 -rw-r--r-- 1 root root 43 Jul 25 09:36 helloworld [root@6365668cea55 dbdata]# cat helloworld hello, world by 5a43b6347b53, who is host? [root@6365668cea55 dbdata]# echo "hello, world by `hostname`, I do not know" >> helloworld [root@6365668cea55 dbdata]# cat helloworld hello, world by 5a43b6347b53, who is host? hello, world by 6365668cea55, I do not know [root@6365668cea55 dbdata]#
Step 4:主機與路人丙
主機什么時候都能看見信息的更新,作為應該回郵件的人,完全有權利裝作看不見
[root@host88 volumes]# pwd /var/lib/docker/volumes [root@host88 volumes]# ll total 0 drwxr-xr-x 3 root root 18 Jul 25 05:31 volname [root@host88 volumes]# find . -type f ./volname/_data/helloworld [root@host88 volumes]# cat ./volname/_data/helloworld hello, world by 5a43b6347b53, who is host? hello, world by 6365668cea55, I do not know [root@host88 volumes]#
路人丙使用ubuntu,他覺得這樣數據設計地實在不好,他表示他根本不想看到這樣的信息,大家不要再reply to all
[root@host88 ~]# docker run -it -v volname:/volumedata/dbdata ubuntu root@730209b03ea6:/# cd volumedata/dbdata root@730209b03ea6:/volumedata/dbdata# ls -l total 4 -rw-r--r-- 1 root root 87 Jul 25 09:44 helloworld root@730209b03ea6:/volumedata/dbdata# cat helloworld hello, world by 5a43b6347b53, who is host? hello, world by 6365668cea55, I do not know root@730209b03ea6:/volumedata/dbdata# echo "hello, world by `hostname`, please do not reply to all" >> helloworld root@730209b03ea6:/volumedata/dbdata# cat helloworld hello, world by 5a43b6347b53, who is host? hello, world by 6365668cea55, I do not know hello, world by 730209b03ea6, please do not reply to all root@730209b03ea6:/volumedata/dbdata#
Step 5:大家都看到了信息,決定都不再說話
作為和現實世界相反的期待,大家覺得這實在太無聊了,于是沒有人再不斷跳出來Reply all說請把我從mail link中剔除
[root@6365668cea55 dbdata]# cat helloworld hello, world by 5a43b6347b53, who is host? hello, world by 6365668cea55, I do not know hello, world by 730209b03ea6, please do not reply to all [root@6365668cea55 dbdata]#
root@5a43b6347b53:/volumedata/dbdata# cat helloworld hello, world by 5a43b6347b53, who is host? hello, world by 6365668cea55, I do not know hello, world by 730209b03ea6, please do not reply to all root@5a43b6347b53:/volumedata/dbdata#
[root@host88 volumes]# cat ./volname/_data/helloworld hello, world by 5a43b6347b53, who is host? hello, world by 6365668cea55, I do not know hello, world by 730209b03ea6, please do not reply to all [root@host88 volumes]#
實際多Container使用同一個Volume完全可以做的更好,把讀寫的權限進行合理設定,能夠滿足很多實際的場景。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
Docker獲取鏡像報錯docker: Error response from daemon
這篇文章主要介紹了Docker獲取鏡像報錯docker: Error response from daemon, 出現了鏡像獲取報錯的問題,找到了解決的方法記一下,需要的朋友可以參考下2018-08-08如何解決報錯unable to remove volume問題
這篇文章主要介紹了如何解決報錯unable to remove volume問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02docker 查詢或獲取私有倉庫(registry)中的鏡像的方法
這篇文章主要介紹了docker 查詢或獲取私有倉庫(registry)中的鏡像的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05詳解從 0 開始使用 Docker 快速搭建 Hadoop 集群環(huán)境
這篇文章主要介紹了詳解從 0 開始使用 Docker 快速搭建 Hadoop 集群環(huán)境,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03