你必須知道的Docker數(shù)據(jù)卷(Volume)
什么是數(shù)據(jù)卷
使用docker容器的時候,會產(chǎn)生一系列的數(shù)據(jù)文件,這些數(shù)據(jù)文件在刪除docker容器時是會消失的,但是其中產(chǎn)生的部分內(nèi)容是希望能夠把它給保存起來另作用途的,Docker將應(yīng)用與運行環(huán)境打包成容器發(fā)布,程序員希望在運行過程鐘產(chǎn)生的部分數(shù)據(jù)是可以持久化的的,而且容器之間我們希望能夠?qū)崿F(xiàn)數(shù)據(jù)共享。數(shù)據(jù)卷是一個可供一個或多個容器使用的特殊目錄,它將主機操作系統(tǒng)目錄直接映射進容器。在容器中修改的內(nèi)容可以在宿主機對應(yīng)的目錄下看到,比如:重要日志 、配置文件等。
數(shù)據(jù)卷的特點
Docker 數(shù)據(jù)卷是 Docker 容器中持久存儲數(shù)據(jù)的機制,具有以下特點:
- 持久性:數(shù)據(jù)卷獨立于容器的生命周期,容器刪除后數(shù)據(jù)卷仍然存在,可以被其他容器掛載和使用。
- 共享性:多個容器可以共享同一個數(shù)據(jù)卷,實現(xiàn)數(shù)據(jù)在容器之間的共享和傳遞。
- 數(shù)據(jù)卷可以提供外部數(shù)據(jù):可以將主機文件系統(tǒng)的目錄或文件掛載為數(shù)據(jù)卷,容器可以直接訪問主機上的數(shù)據(jù)。
- 容器之間隔離:即使多個容器共享同一個數(shù)據(jù)卷,它們之間的操作仍然是相互隔離的,不會相互影響。
- 高性能:與將數(shù)據(jù)存儲在容器內(nèi)部相比,使用數(shù)據(jù)卷通常具有更高的性能,因為數(shù)據(jù)卷可以利用主機文件系統(tǒng)的優(yōu)勢。
- 可備份和恢復(fù):可以輕松備份和恢復(fù)數(shù)據(jù)卷中的數(shù)據(jù),方便進行數(shù)據(jù)管理和遷移。
通過使用數(shù)據(jù)卷,Docker 提供了一種靈活且持久的方式來管理容器中的數(shù)據(jù),使數(shù)據(jù)在容器之間共享和持久化成為可能。
Docker數(shù)據(jù)卷操作
管理卷
列出所有卷
docker volume 命令可以對 Docker 自己管理的卷(/var/lib/docker/volumes/xx)目錄進行操作。
[root@localhost]~ docker volume ls
DRIVER VOLUME NAME
local 2f3bf43b086338934a1d1664ebd0bb17828eb92c1253c25c4a73254fd8e4663d
local ced2e5231eda1a01664b8d274ce5ada4ba6361744e475188ea4a0ea352143e18
local d1169326a7b04b649a17ec2c3c9b35d2a6fe093813dcd5bb56ed9ad3a67803fd
創(chuàng)建卷
[root@localhost]~ docker volume create test
test
查詢卷詳情
[root@localhost]~ docker volume inspect test
[
{
"CreatedAt": "2023-10-05T08:44:42+08:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/test/_data",
"Name": "test",
"Options": {},
"Scope": "local"
}
]
刪除卷
[root@localhost]~ docker volume rm test
test
移除無用卷
[root@localhost]~ docker volume prune
WARNING! This will remove all local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
卷掛載
匿名卷
什么也不需要寫,也不要加冒號,直接寫容器內(nèi)的目錄 實際上是系統(tǒng)自動生成一個卷的名字
# Docker 將創(chuàng)建出匿名卷,并保存容器 /usr/share/nginx/html 下面的內(nèi)容
[root@localhost]~ docker run -d --name nginx -P -v /usr/share/nginx/html nginx
c51638f465c5bd4753473663520122714108f406fac575d95bf72430ec4b6b07
查看容器
[root@localhost]~ docker inspect nginx
...
"Mounts": [
{
"Type": "volume",
"Name": "b24408483f4adc0decfbc66787dd0534dab86bcb4715d7e166361b332a4e697c",
"Source": "/var/lib/docker/volumes/b24408483f4adc0decfbc66787dd0534dab86bcb4715d7e166361b332a4e697c/_data",
"Destination": "/usr/share/nginx/html",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
...
查看所有volume
[root@localhost]~ docker volume ls
DRIVER VOLUME NAME
local 2f3bf43b086338934a1d1664ebd0bb17828eb92c1253c25c4a73254fd8e4663d
local b24408483f4adc0decfbc66787dd0534dab86bcb4715d7e166361b332a4e697c
local ced2e5231eda1a01664b8d274ce5ada4ba6361744e475188ea4a0ea352143e18
local d1169326a7b04b649a17ec2c3c9b35d2a6fe093813dcd5bb56ed9ad3a67803fd
可以看到剛剛創(chuàng)建的nginx容器對應(yīng)的是 b24408483f4adc0decfbc66787dd0534dab86bcb4715d7e166361b332a4e697c volume 進入到 目錄 可以看到容器內(nèi)的數(shù)據(jù)
[root@localhost]~ cd /var/lib/docker/volumes/b24408483f4adc0decfbc66787dd0534dab86bcb4715d7e166361b332a4e697c/_data/
[root@localhost]~ ls -l
total 8
-rw-r--r-- 1 root root 497 Aug 16 01:03 50x.html
-rw-r--r-- 1 root root 615 Aug 16 01:03 index.html
測試持久化,進入容器內(nèi)目錄創(chuàng)建test文件
[root@localhost]~ docker exec -it nginx sh
# cd /usr/share/nginx/html/ && touch test
# ls -l
total 8
-rw-r--r-- 1 root root 497 Aug 15 17:03 50x.html
-rw-r--r-- 1 root root 615 Aug 15 17:03 index.html
-rw-r--r-- 1 root root 0 Dec 5 01:16 test
回到宿主機查看
[root@localhost]~ ls -l /var/lib/docker/volumes/b24408483f4adc0decfbc66787dd0534dab86bcb4715d7e166361b332a4e697c/_data
total 8
-rw-r--r-- 1 root root 497 Aug 16 01:03 50x.html
-rw-r--r-- 1 root root 615 Aug 16 01:03 index.html
-rw-r--r-- 1 root root 0 Dec 5 09:16 test
具名卷
首先創(chuàng)建一個 volume nginx
[root@localhost]~ docker volume create nginx
nginx
使用具名卷映射
[root@localhost]~ docker run -d --name nginx -P -v nginx:/usr/share/nginx/html nginx
290dc693dar21r2335tgbfdbnfgADADGT32d222c7f4c4fc01ecfc670628c6d41517ea532b
查看容器
[root@localhost]~ docker inspect nginx
...
"Mounts": [
{
"Type": "volume",
"Name": "nginx",
"Source": "/var/lib/docker/volumes/nginx/_data",
"Destination": "/usr/share/nginx/html",
"Driver": "local",
"Mode": "z",
"RW": true,
"Propagation": ""
}
],
...
查看所有volume
[root@localhost]~ docker volume ls
DRIVER VOLUME NAME
local 2f3bf43b086338934a1d1664ebd0bb17828eb92c1253c25c4a73254fd8e4663d
local ced2e5231eda1a01664b8d274ce5ada4ba6361744e475188ea4a0ea352143e18
local d1169326a7b04b649a17ec2c3c9b35d2a6fe093813dcd5bb56ed9ad3a67803fd
local nginx
可以看到剛剛創(chuàng)建的nginx容器對應(yīng)的是 nginx volume 進入到 目錄 可以看到容器內(nèi)的數(shù)據(jù)
[root@localhost]~ cd /var/lib/docker/volumes/nginx/_data
[root@localhost]~ ls -l
total 8
-rw-r--r-- 1 root root 497 Aug 16 01:03 50x.html
-rw-r--r-- 1 root root 615 Aug 16 01:03 index.html
測試持久化,進入容器內(nèi)目錄創(chuàng)建test文件
[root@localhost]~ docker exec -it nginx sh
# cd /usr/share/nginx/html/ && touch test
# ls -l
total 8
-rw-r--r-- 1 root root 497 Aug 15 17:03 50x.html
-rw-r--r-- 1 root root 615 Aug 15 17:03 index.html
-rw-r--r-- 1 root root 0 Dec 5 01:16 test
回到宿主機查看
[root@localhost]~ ls -l /var/lib/docker/volumes/nginx/_data
total 8
-rw-r--r-- 1 root root 497 Aug 16 01:03 50x.html
-rw-r--r-- 1 root root 615 Aug 16 01:03 index.html
-rw-r--r-- 1 root root 0 Dec 5 09:16 test
刪除容器重新創(chuàng)建
[root@localhost]~ docker rm -f nginx
[root@localhost]~ docker run -d --name nginx -P -v nginx:/usr/share/nginx/html nginx
進入容器內(nèi)查看數(shù)據(jù)
[root@localhost]~ docker exec -it nginx sh
# ls -l /usr/share/nginx/html
total 8
-rw-r--r-- 1 root root 497 Aug 15 17:03 50x.html
-rw-r--r-- 1 root root 615 Aug 15 17:03 index.html
-rw-r--r-- 1 root root 0 Dec 5 01:20 test
持久化保存成功
綁定掛載(bind)
將本地主機的 path 映射到 容器里
[root@localhost]~ docker run -d --name nginx -P -v /tmp/nginx:/usr/share/nginx/html nginx
290dc693c156a28e34160fbce8d222c7f4c4fc01ecfc670628c6d41517ea532b
查看容器
[root@localhost]~ docker inspect nginx
...
"Mounts": [
{
"Type": "bind",
"Source": "/tmp/nginx",
"Destination": "/usr/share/nginx/html",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
...
查看所有volume,可以看出來沒有多出來的 volume name
[root@localhost]~ docker volume ls
DRIVER VOLUME NAME
local 2f3bf43b086338934a1d1664ebd0bb17828eb92c1253c25c4a73254fd8e4663d
local ced2e5231eda1a01664b8d274ce5ada4ba6361744e475188ea4a0ea352143e18
local d1169326a7b04b649a17ec2c3c9b35d2a6fe093813dcd5bb56ed9ad3a67803fd
進入到主機 目錄 看不到容器內(nèi)的數(shù)據(jù), 需要注意的是
使用 bind 方式做數(shù)據(jù)卷的映射時,首次 docker run -v 運行,如果本機的文件夾是沒有內(nèi)容的,docker容器中的文件夾是有內(nèi)容的,則本機的會覆蓋dokcer容器中的,也就是容器中原本有內(nèi)容的也會沒有內(nèi)容
如果本機的文件夾是有內(nèi)容的,docker容器中的文件夾是有內(nèi)容的,則本機的會覆蓋dokcer容器中的 由于宿主機上 /tmp/nginx 這個目錄底下沒有文件,所以容器內(nèi)的數(shù)據(jù)會被主機目錄覆蓋清空。
[root@localhost]~ cd /tmp/nginx
[root@localhost]~ ls -l
total 0
測試持久化,進入容器內(nèi)目錄創(chuàng)建test文件
[root@localhost]~ docker exec -it nginx sh
# cd /usr/share/nginx/html/ && touch test
# ls -l
test
回到宿主機查看
[root@localhost]~ ls -l /tmp/nginx
total 0
-rw-r--r-- 1 root root 0 Dec 5 10:25 test
刪除容器重新創(chuàng)建
[root@localhost]~ docker rm -f nginx
[root@localhost]~ docker run -d --name nginx -P -v /tmp/nginx:/usr/share/nginx/html nginx
進入容器內(nèi)查看數(shù)據(jù)
[root@localhost]~ docker exec -it nginx sh
# ls -l /usr/share/nginx/html
total 0
-rw-r--r-- 1 root root 0 Dec 5 02:25 test
持久化保存成功