轉自:https://blog.csdn.net/liumiaocn/article/details/52028007docker
Docker中可使用Named volume和data container來進行數據的管理。ubuntu
事前確認volume的信息,沒有VOLUME存在centos
[root@host88 volumes]# docker volume ls
DRIVER VOLUME NAME
確認/var/lib/docker/volumes的情況this
[root@host88 volumes]# pwd
/var/lib/docker/volumes
[root@host88 volumes]# ll
total 0
建立一個名爲volname的數據卷,經過-v參數能夠進行建立,同時也能夠經過docker volume create來建立。.net
[root@host88 volumes]# docker run -it -v volname:/volumedata/dbdata debian
root@b2e3523a6dd9:/# cd volumedata/dbdata
root@b2e3523a6dd9:/volumedata/dbdata# ls -l
total 0
在Container外部確認此事volname是否已經建立成功設計
[root@host88 volumes]# docker volume ls
DRIVER VOLUME NAME
local volname
確認/var/lib/docker/volumes下面 的狀況3d
[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
除了目錄結構沒有任何文件存在blog
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
在外部確認該信息是否已經存在it
[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]# 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
在內部確認信息io
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
從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# 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
數據仍然存在。使用docker volume ls能夠看到剛剛volname的數據卷也依然存在。
[root@host88 volumes]# docker volume ls
DRIVER VOLUME NAME
local volname
docker的volume的管理目前主要有下面4種:create/ls/inspect/rm
[root@host88 volumes]# docker volume ls
DRIVER VOLUME NAME
local volname
[root@host88 volumes]#
正常的環境必定不會跑出這麼清靜的結果。
[root@host88 volumes]# docker volume inspect volname
[
{
"Name": "volname",
"Driver": "local",
"Mountpoint": "/var/lib/docker/volumes/volname/_data"
}
]
其實這個信息可能會以爲很是眼熟,看完docker insepect 的結果就會發現,內容是一致的,如下是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"
}
],
刪除以前的確認
[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
刪除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環境中,成爲殭屍的不僅是/var/lib/docker/volumes下面的實際數據
並且docker volume ls中也會有不少徹底不知道的信息,甚至有些相關聯的實際數據已經被刪除
這種狀況在不少考慮不足的環境中家常便飯,雖然只是很簡單的helloworld
數據管理時候須要考慮的問題仍是值得引發足夠的重視。
能夠像例子那樣經過run 和-v建立volume,同時也可使用docker volume create來建立
[root@host88 volumes]# docker volume create --driver=local --name=volname
volname
[root@host88 volumes]# docker volume ls
DRIVER VOLUME NAME
local volname
有些volume在建立時還要結合使用–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]# docker volume rm $(docker volume ls -q)
volname
e54a0022fdff1e0e57b8635317e0b51b1e36c3c9b8c48a051e7778a45f08a83d
須要注意的是這個名字必須是惟一的,因此前面也說到過不使用docker volume rm來刪除的話會致使問題,
下次用一樣名字想要建立一個volume卻發現已經存在的時候就只能是建立失敗了。
用你喜歡的方式建立一個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]# docker run -it -v volname:/volumedata/dbdata debian
路人甲使用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@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?
同時路人乙也與該volume進行了鏈接
[root@host88 ~]# docker run -it -v volname:/volumedata/dbdata centos
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@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
路人丙使用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
做爲和現實世界相反的期待,你們以爲這實在太無聊了,因而沒有人再不斷跳出來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@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@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
實際多Container使用同一個Volume徹底能夠作的更好,把讀寫的權限進行合理設定,可以知足不少實際的場景。