3

你必须知道的Docker数据卷(Volume)

 4 weeks ago
source link: https://www.51cto.com/article/784964.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

什么是数据卷

使用docker容器的时候,会产生一系列的数据文件,这些数据文件在删除docker容器时是会消失的,但是其中产生的部分内容是希望能够把它给保存起来另作用途的,Docker将应用与运行环境打包成容器发布,程序员希望在运行过程钟产生的部分数据是可以持久化的的,而且容器之间我们希望能够实现数据共享。数据卷是一个可供一个或多个容器使用的特殊目录,它将主机操作系统目录直接映射进容器。在容器中修改的内容可以在宿主机对应的目录下看到,比如:重要日志 、配置文件等。

数据卷的特点

Docker 数据卷是 Docker 容器中持久存储数据的机制,具有以下特点:

  1. 持久性:数据卷独立于容器的生命周期,容器删除后数据卷仍然存在,可以被其他容器挂载和使用。
  2. 共享性:多个容器可以共享同一个数据卷,实现数据在容器之间的共享和传递。
  3. 数据卷可以提供外部数据:可以将主机文件系统的目录或文件挂载为数据卷,容器可以直接访问主机上的数据。
  4. 容器之间隔离:即使多个容器共享同一个数据卷,它们之间的操作仍然是相互隔离的,不会相互影响。
  5. 高性能:与将数据存储在容器内部相比,使用数据卷通常具有更高的性能,因为数据卷可以利用主机文件系统的优势。
  6. 可备份和恢复:可以轻松备份和恢复数据卷中的数据,方便进行数据管理和迁移。

通过使用数据卷,Docker 提供了一种灵活且持久的方式来管理容器中的数据,使数据在容器之间共享和持久化成为可能。

Docker数据卷操作

列出所有卷

docker volume 命令可以对 Docker 自己管理的卷(/var/lib/docker/volumes/xx)目录进行操作。

[root@localhost]~ docker volume ls
DRIVER    VOLUME NAME
local     2f3bf43b086338934a1d1664ebd0bb17828eb92c1253c25c4a73254fd8e4663d
local     ced2e5231eda1a01664b8d274ce5ada4ba6361744e475188ea4a0ea352143e18
local     d1169326a7b04b649a17ec2c3c9b35d2a6fe093813dcd5bb56ed9ad3a67803fd
[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

什么也不需要写,也不要加冒号,直接写容器内的目录 实际上是系统自动生成一个卷的名字

# Docker 将创建出匿名卷,并保存容器 /usr/share/nginx/html 下面的内容
[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

可以看到刚刚创建的nginx容器对应的是  b24408483f4adc0decfbc66787dd0534dab86bcb4715d7e166361b332a4e697c volume 进入到 目录 可以看到容器内的数据

[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

测试持久化,进入容器内目录创建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

首先创建一个 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

可以看到刚刚创建的nginx容器对应的是  nginx volume 进入到 目录 可以看到容器内的数据

[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

测试持久化,进入容器内目录创建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

删除容器重新创建

[root@localhost]~ docker rm -f  nginx
[root@localhost]~ docker run -d --name nginx -P -v nginx:/usr/share/nginx/html nginx

进入容器内查看数据

[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

进入到主机 目录 看不到容器内的数据, 需要注意的是

使用 bind 方式做数据卷的映射时,首次 docker run -v 运行,如果本机的文件夹是没有内容的,docker容器中的文件夹是有内容的,则本机的会覆盖dokcer容器中的,也就是容器中原本有内容的也会没有内容

如果本机的文件夹是有内容的,docker容器中的文件夹是有内容的,则本机的会覆盖dokcer容器中的 由于宿主机上 /tmp/nginx 这个目录底下没有文件,所以容器内的数据会被主机目录覆盖清空。

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

测试持久化,进入容器内目录创建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

删除容器重新创建

[root@localhost]~ docker rm -f nginx
[root@localhost]~ docker run -d --name nginx -P -v /tmp/nginx:/usr/share/nginx/html nginx

进入容器内查看数据

[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

持久化保存成功


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK