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

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

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

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

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

單一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的數(shù)據(jù)卷,通過-v參數(shù)可以進行創(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是否已經(jīng)創(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]#

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

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#

在外部確認該信息是否已經(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)部確認信息

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)境一定不會跑出這么清靜的結(jié)果。

inspect

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

其實這個信息可能會覺得非常眼熟,看完docker insepect 的結(jié)果就會發(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)

刪除之前的確認

[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之前需要刪除與其有依賴關(guān)系的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下面的實際數(shù)據(jù)

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

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

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

創(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)建時還要結(jié)合使用–opt參數(shù)(或者-o)

如果不指定–name參數(shù),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ā)現(xiàn)已經(jīng)存在的時候就只能是創(chuàng)建失敗了。

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

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現(xiàn)在不能使用相對路徑,所以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:主機與路人丙

主機什么時候都能看見信息的更新,作為應(yīng)該回郵件的人,完全有權(quá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,他覺得這樣數(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:大家都看到了信息,決定都不再說話

作為和現(xiàn)實世界相反的期待,大家覺得這實在太無聊了,于是沒有人再不斷跳出來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完全可以做的更好,把讀寫的權(quán)限進行合理設(shè)定,能夠滿足很多實際的場景。

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

相關(guān)文章

最新評論