欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Docker 數(shù)據(jù)管理Named volume詳解

 更新時(shí)間:2017年03月20日 16:03:43   投稿:lqh  
這篇文章主要介紹了Docker 數(shù)據(jù)管理Named volume詳解的相關(guān)資料,需要的朋友可以參考下

Docker數(shù)據(jù)管理:Named volume

Docker中可以使用Named volume和data container來(lái)進(jìn)行數(shù)據(jù)的管理。

單一Container的使用Helloworld

Step 1:創(chuàng)建一個(gè)Named Volume

事前確認(rèn)volume的信息,沒(méi)有VOLUME存在

[root@host88 volumes]# docker volume ls
DRIVER    VOLUME NAME
[root@host88 volumes]#

確認(rèn)/var/lib/docker/volumes的狀況

[root@host88 volumes]# pwd
/var/lib/docker/volumes
[root@host88 volumes]# ll
total 0
[root@host88 volumes]#

創(chuàng)建一個(gè)名為volname的數(shù)據(jù)卷,通過(guò)-v參數(shù)可以進(jìn)行創(chuàng)建,同時(shí)也可以通過(guò)docker volume create來(lái)創(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外部確認(rèn)此事volname是否已經(jīng)創(chuàng)建成功

[root@host88 volumes]# docker volume ls
DRIVER    VOLUME NAME
local    volname
[root@host88 volumes]#

確認(rèn)/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]#

除了目錄結(jié)構(gòu)沒(méi)有任何文件存在

Step 2:在Container中保存數(shù)據(jù)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#

在外部確認(rèn)該信息是否已經(jīng)存在

[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]#

在內(nèi)部確認(rèn)信息

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后看數(shù)據(jù)是否仍然存在

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]#

數(shù)據(jù)仍然存在。使用docker volume ls可以看到剛剛volname的數(shù)據(jù)卷也依然存在。

[root@host88 volumes]# docker volume ls
DRIVER    VOLUME NAME
local    volname
[root@host88 volumes]#

數(shù)據(jù)卷的管理

docker的volume的管理目前主要有下面4種:create/ls/inspect/rm

查詢(ls)

[root@host88 volumes]# docker volume ls
DRIVER    VOLUME NAME
local    volname
[root@host88 volumes]#

正常的環(huán)境一定不會(huì)跑出這么清靜的結(jié)果。

inspect

[root@host88 volumes]# docker volume inspect volname
[
 {
  "Name": "volname",
  "Driver": "local",
  "Mountpoint": "/var/lib/docker/volumes/volname/_data"
 }
]
[root@host88 volumes]#

其實(shí)這個(gè)信息可能會(huì)覺(jué)得非常眼熟,看完docker insepect 的結(jié)果就會(huì)發(fā)現(xiàn),內(nèi)容是一致的,以下是docker inspect b2e3523a6dd9的mounts相關(guān)信息

  "Mounts": [
   {
    "Name": "volname",
    "Source": "/var/lib/docker/volumes/volname/_data",
    "Destination": "/volumedata/dbdata",
    "Driver": "local",
    "Mode": "z",
    "RW": true,
    "Propagation": "rslave"
   }
  ],

刪除(rm)

刪除之前的確認(rèn)

[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之前需要?jiǎng)h除與其有依賴關(guān)系的container

[root@host88 volumes]# docker rm b2e3523a6dd9
b2e3523a6dd9
[root@host88 volumes]#

刪除container并不會(huì)將volume一并刪除

[root@host88 volumes]# pwd
/var/lib/docker/volumes
[root@host88 volumes]# find . -type f
./volname/_data/helloworld
[root@host88 volumes]#

而使用docker volume rm則會(huì)干凈地刪除掉所有信息

[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]#

長(zhǎng)時(shí)間運(yùn)行的Docker環(huán)境中,成為僵尸的不只是/var/lib/docker/volumes下面的實(shí)際數(shù)據(jù)

而且docker volume ls中也會(huì)有很多完全不知道的信息,甚至有些相關(guān)聯(lián)的實(shí)際數(shù)據(jù)已經(jīng)被刪除

這種情況在很多考慮不足的環(huán)境中屢見(jiàn)不鮮,雖然只是很簡(jiǎn)單的helloworld

數(shù)據(jù)管理時(shí)候需要考慮的問(wèn)題還是值得引起足夠的重視。

創(chuàng)建(create):

可以像例子那樣通過(guò)run 和-v創(chuàng)建volume,同時(shí)也可以使用docker volume create來(lái)創(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)建時(shí)還要結(jié)合使用–opt參數(shù)(或者-o)

如果不指定–name參數(shù),docker會(huì)體貼地替你取一個(gè),大概就像下面這樣

[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]#

需要注意的是這個(gè)名字必須是唯一的,所以前面也說(shuō)到過(guò)不使用docker volume rm來(lái)刪除的話會(huì)導(dǎo)致問(wèn)題,

下次用同樣名字想要?jiǎng)?chuàng)建一個(gè)volume卻發(fā)現(xiàn)已經(jīng)存在的時(shí)候就只能是創(chuàng)建失敗了。

多Container共用一個(gè)數(shù)據(jù)卷

Step 1:創(chuàng)建一個(gè)Named Volume

用你喜歡的方式創(chuàng)建一個(gè)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,他想知道誰(shuí)是docker的主機(jī)

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:主機(jī)與路人乙

主機(jī)此時(shí)看到了這個(gè)信息

[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]#

同時(shí)路人乙也與該volume進(jìn)行了連接

[root@host88 ~]# docker run -it -v volname:/volumedata/dbdata centos
[root@6365668cea55 /]#

BTW,Docker現(xiàn)在不能使用相對(duì)路徑,所以volname:/volumedata/dbdata的這個(gè)寫(xiě)法最前面的/仍然是不可或缺.
路人乙說(shuō)雖然你不是找我,但是我看見(jiàn)了,這是共享的,我就可以回信么,說(shuō)我不知道。

[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:主機(jī)與路人丙

主機(jī)什么時(shí)候都能看見(jiàn)信息的更新,作為應(yīng)該回郵件的人,完全有權(quán)利裝作看不見(jiàn)

[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,他覺(jué)得這樣數(shù)據(jù)設(shè)計(jì)地實(shí)在不好,他表示他根本不想看到這樣的信息,大家不要再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:大家都看到了信息,決定都不再說(shuō)話

作為和現(xiàn)實(shí)世界相反的期待,大家覺(jué)得這實(shí)在太無(wú)聊了,于是沒(méi)有人再不斷跳出來(lái)Reply all說(shuō)請(qǐng)把我從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]#

實(shí)際多Container使用同一個(gè)Volume完全可以做的更好,把讀寫(xiě)的權(quán)限進(jìn)行合理設(shè)定,能夠滿足很多實(shí)際的場(chǎng)景。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論