自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

你必須知道的Docker數(shù)據(jù)卷(Volume)

云計(jì)算 云原生
數(shù)據(jù)卷是一個(gè)可供一個(gè)或多個(gè)容器使用的特殊目錄,它將主機(jī)操作系統(tǒng)目錄直接映射進(jìn)容器。在容器中修改的內(nèi)容可以在宿主機(jī)對(duì)應(yīng)的目錄下看到,比如:重要日志 、配置文件等。

什么是數(shù)據(jù)卷

使用docker容器的時(shí)候,會(huì)產(chǎn)生一系列的數(shù)據(jù)文件,這些數(shù)據(jù)文件在刪除docker容器時(shí)是會(huì)消失的,但是其中產(chǎn)生的部分內(nèi)容是希望能夠把它給保存起來(lái)另作用途的,Docker將應(yīng)用與運(yùn)行環(huán)境打包成容器發(fā)布,程序員希望在運(yùn)行過(guò)程鐘產(chǎn)生的部分?jǐn)?shù)據(jù)是可以持久化的的,而且容器之間我們希望能夠?qū)崿F(xiàn)數(shù)據(jù)共享。數(shù)據(jù)卷是一個(gè)可供一個(gè)或多個(gè)容器使用的特殊目錄,它將主機(jī)操作系統(tǒng)目錄直接映射進(jìn)容器。在容器中修改的內(nèi)容可以在宿主機(jī)對(duì)應(yīng)的目錄下看到,比如:重要日志 、配置文件等。

數(shù)據(jù)卷的特點(diǎn)

Docker 數(shù)據(jù)卷是 Docker 容器中持久存儲(chǔ)數(shù)據(jù)的機(jī)制,具有以下特點(diǎn):

  1. 持久性:數(shù)據(jù)卷獨(dú)立于容器的生命周期,容器刪除后數(shù)據(jù)卷仍然存在,可以被其他容器掛載和使用。
  2. 共享性:多個(gè)容器可以共享同一個(gè)數(shù)據(jù)卷,實(shí)現(xiàn)數(shù)據(jù)在容器之間的共享和傳遞。
  3. 數(shù)據(jù)卷可以提供外部數(shù)據(jù):可以將主機(jī)文件系統(tǒng)的目錄或文件掛載為數(shù)據(jù)卷,容器可以直接訪問(wèn)主機(jī)上的數(shù)據(jù)。
  4. 容器之間隔離:即使多個(gè)容器共享同一個(gè)數(shù)據(jù)卷,它們之間的操作仍然是相互隔離的,不會(huì)相互影響。
  5. 高性能:與將數(shù)據(jù)存儲(chǔ)在容器內(nèi)部相比,使用數(shù)據(jù)卷通常具有更高的性能,因?yàn)閿?shù)據(jù)卷可以利用主機(jī)文件系統(tǒng)的優(yōu)勢(shì)。
  6. 可備份和恢復(fù):可以輕松備份和恢復(fù)數(shù)據(jù)卷中的數(shù)據(jù),方便進(jìn)行數(shù)據(jù)管理和遷移。

通過(guò)使用數(shù)據(jù)卷,Docker 提供了一種靈活且持久的方式來(lái)管理容器中的數(shù)據(jù),使數(shù)據(jù)在容器之間共享和持久化成為可能。

Docker數(shù)據(jù)卷操作

管理卷

列出所有卷

docker volume 命令可以對(duì) Docker 自己管理的卷(/var/lib/docker/volumes/xx)目錄進(jìn)行操作。

[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

移除無(wú)用卷

[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

卷掛載

匿名卷

什么也不需要寫,也不要加冒號(hào),直接寫容器內(nèi)的目錄 實(shí)際上是系統(tǒng)自動(dòng)生成一個(gè)卷的名字

# 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容器對(duì)應(yīng)的是  b24408483f4adc0decfbc66787dd0534dab86bcb4715d7e166361b332a4e697c volume 進(jìn)入到 目錄 可以看到容器內(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

測(cè)試持久化,進(jìn)入容器內(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

回到宿主機(jī)查看

[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)建一個(gè) 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容器對(duì)應(yīng)的是  nginx volume 進(jìn)入到 目錄 可以看到容器內(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

測(cè)試持久化,進(jìn)入容器內(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

回到宿主機(jī)查看

[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

進(jìn)入容器內(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)

將本地主機(jī)的 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,可以看出來(lái)沒(méi)有多出來(lái)的 volume name

[root@localhost]~ docker volume ls
DRIVER    VOLUME NAME
local     2f3bf43b086338934a1d1664ebd0bb17828eb92c1253c25c4a73254fd8e4663d
local     ced2e5231eda1a01664b8d274ce5ada4ba6361744e475188ea4a0ea352143e18
local     d1169326a7b04b649a17ec2c3c9b35d2a6fe093813dcd5bb56ed9ad3a67803fd

進(jìn)入到主機(jī) 目錄 看不到容器內(nèi)的數(shù)據(jù), 需要注意的是

使用 bind 方式做數(shù)據(jù)卷的映射時(shí),首次 docker run -v 運(yùn)行,如果本機(jī)的文件夾是沒(méi)有內(nèi)容的,docker容器中的文件夾是有內(nèi)容的,則本機(jī)的會(huì)覆蓋dokcer容器中的,也就是容器中原本有內(nèi)容的也會(huì)沒(méi)有內(nèi)容

如果本機(jī)的文件夾是有內(nèi)容的,docker容器中的文件夾是有內(nèi)容的,則本機(jī)的會(huì)覆蓋dokcer容器中的 由于宿主機(jī)上 /tmp/nginx 這個(gè)目錄底下沒(méi)有文件,所以容器內(nèi)的數(shù)據(jù)會(huì)被主機(jī)目錄覆蓋清空。

[root@localhost]~ cd /tmp/nginx
[root@localhost]~ ls -l
total 0

測(cè)試持久化,進(jìn)入容器內(nèi)目錄創(chuàng)建test文件

[root@localhost]~ docker exec -it nginx sh
# cd /usr/share/nginx/html/ && touch test
# ls -l
test

回到宿主機(jī)查看

[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

進(jìn)入容器內(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

持久化保存成功

責(zé)任編輯:武曉燕 來(lái)源: 云原生運(yùn)維圈
相關(guān)推薦

2017-12-07 15:47:25

2020-02-28 14:05:00

Linuxshell命令

2012-09-29 09:22:24

.NETGC內(nèi)存分配

2012-09-29 10:29:56

.Net內(nèi)存分配繼承

2017-12-07 15:28:36

2021-10-29 08:44:22

推拉機(jī)制面試broker

2011-11-30 09:09:13

王濤Windows Pho移動(dòng)開發(fā)

2015-06-29 09:40:10

Rails新特性

2017-10-11 15:50:18

光纖通信傳輸

2015-07-23 10:37:13

Linux命令

2021-03-01 07:34:42

Java泛型ArrayList

2020-12-29 09:50:23

大數(shù)據(jù)大數(shù)據(jù)技術(shù)

2012-11-05 09:19:37

2011-05-11 15:28:05

2019-05-30 08:25:50

5G4G網(wǎng)絡(luò)

2011-12-16 17:05:58

2015-10-27 10:22:47

Html5API調(diào)用

2019-01-08 10:29:12

BeautifulSoPython第三庫(kù)

2012-02-08 09:44:05

ChromeAndroid

2011-05-13 11:41:55

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)