目录
- 写在前面
- 几种存储挂载方式
- 1.bind mounts
- 2.volumes
- 3.tmpfs mount
- 存储数据共享
写在前面
继上一篇介绍容器网络驱动Bridge的工作原理后,本节聚焦于容器技术的另一个核心领域——存储机制。
容器的存储体系主要划分为两类:
第一类是与镜像紧密相关的容器层,基于《Docker容器技术基础之联合文件系统OverlayFS》中提到的Copy-On-Write特性。默认情况下,容器内新建的文件均写入可写的容器层。这种方式导致数据难以持久化和共享,且因涉及存储驱动抽象及直接写入主机文件系统,性能低于直接使用数据卷。
第二类是宿主机存储,即将宿主机的目录绑定或挂载至容器中,确保容器停止后数据依然保留。本文将重点阐述此类存储方式。
几种存储挂载方式
根据数据存储位置的不同,Docker主机上的存储结构如下图所示:

1.bind mounts
相较于卷,绑定挂载功能较为有限。它直接将主机上的文件或目录挂载到容器内,引用方式为主机上的完整绝对路径。若指定目录在主机上不存在,Docker会自动创建该目录(注意:仅自动创建目录)。
使用-v选项将主机目录/nginx/html挂载到容器中进行测试:
docker run -dt -v /nginx/html:/usr/share/nginx/html --name nginx nginx
执行docker inspect nginx命令,查看输出中的Mounts字段以确认挂载状态。
"Mounts": [
{
"Type": "bind",
"Source": "/nginx/html",
"Destination": "/usr/share/nginx/html",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
随后在主机上创建index.html并写入“hello nginx”,访问容器IP验证发现挂载已生效。
[root@localhost ~]# echo "hello nginx" > /nginx/html/index.html
[root@localhost ~]# curl 172.17.0.4
hello nginx
存在双向修改风险:主机修改文件会同步至容器,反之容器也可修改、创建或删除主机文件系统内容。为避免此问题,可在创建容器时设置挂载权限,例如只读模式:
docker run -dt -v /nginx/html:/usr/share/nginx/html:ro --name nginx nginx
使用绑定挂载时,操作的是主机文件系统,必须明确以下两点:
- 挂载目录包含的具体内容,防止影响其他应用。
- 容器是否具备操作这些目录的必要权限。
2.volumes
Volume存储卷由Docker创建和管理。可通过docker volume create命令显式创建,或在创建容器时自动生成。
[root@localhost ~]# docker volume create nginx_volume
nginx_volume
[root@localhost volumes]# docker inspect nginx_volume
[
{
"CreatedAt": "2026-08-12T01:58:04-04:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/nginx_volume/_data",
"Name": "nginx_volume",
"Options": {},
"Scope": "local"
}
]
挂载点通常位于Docker根目录/var/lib/docker/volumes下。
利用docker volume rm/prune命令可清除单个或所有未使用的卷。这种通过Docker命令管理卷的能力,是区别于绑定挂载的一大优势。
[root@localhost ~]# docker volume ls
DRIVER VOLUME NAME
local owncloud-docker-server_files
local owncloud-docker-server_mysql
local owncloud-docker-server_redis
[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
Deleted Volumes:
owncloud-docker-server_files
owncloud-docker-server_mysql
owncloud-docker-server_redis
Total reclaimed space: 199.4MB
若在创建容器时未指定挂载源,Docker会自动创建一个匿名卷,同样存放于Docker根目录下。
[root@localhost volumes]# docker run -dt -v /usr/share/nginx/html --name nginx_with_volume nginx
d25bdfce9c7ac7bde5ae35067f6d9cf9f0cd2c9cbea6d1bbd7127b3949ef5ac6
[root@localhost volumes]# docker volume ls
DRIVER VOLUME NAME
local d8e943f57d17a255f8a4ac3ecbd6471a735aa64cc7a606c52f61319a6c754980
local nginx_volume
[root@localhost volumes]# ls /var/lib/docker/volumes/
backingFsBlockDev d8e943f57d17a255f8a4ac3ecbd6471a735aa64cc7a606c52f61319a6c754980 metadata.db nginx_volume
虽然挂载后的存储行为与bind mounts一致,但当主机缺乏特定的目录或文件结构时,卷能将Docker主机的配置与容器运行时分离。这使得数据在不同Docker主机间的备份、还原或迁移更加便捷,摆脱了host path的限制。
在使用绑定挂载和卷时,需注意传播覆盖原则:

挂载空卷时:容器内目录的内容会被复制传播到卷中。
挂载绑定目录或非空卷时:容器内目录的内容会被卷或绑定的主机目录覆盖。
3.tmpfs mount
Tmpfs挂载仅适用于Linux主机。使用该方式创建的容器可在可写层之外生成文件,数据保留在内存中。容器停止后,写入的数据将被清除。此方式主要用于临时存储敏感文件,避免数据留在主机或容器可写层中。
通过--tmpfs选项挂载一个内存块:
docker run -dt --name busybox_tmpfs --tmpfs /etc/running busybox
通过--mount参数指定临时存储大小:
docker run -dt --name busybox_tmpfs2 --mount type=tmpfs,tmpfs-size=2048,destination=/etc/running busybox
存储数据共享
容器间共享数据主要有两种方法。第一种是将目录或volume挂载到多个容器中,此处不再赘述。下面介绍通过中间容器实现共享的方式。
首先创建一个中间容器,包含绑定挂载目录和一个卷:
docker create -v /share:/volume1 -v /volume2 --name volume_share busybox
在需要共享数据的容器中,通过--volumes-from选项继承使用:
docker run -d -t --volumes-from volume_share --name container1 busybox
执行inspect检查Mounts字段,此时container1已成功挂载到一个bind目录和一个volume。
"Mounts": [
{
"Type": "bind",
"Source": "/share",
"Destination": "/volume1",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "volume",
"Name": "21605e49a0ba90a1b952a32c1b3f0d42735da8bfe718f0dc76c37e91f1e51c0e",
"Source": "/var/lib/docker/volumes/21605e49a0ba90a1b952a32c1b3f0d42735da8bfe718f0dc76c37e91f1e51c0e/_data",
"Destination": "/volume2",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
关于Docker容器存储的基础知识先介绍至此,希望本文能为您提供帮助。
到此这篇关于Docker容器几种存储详解的文章就介绍到这了,更多相关Docker存储内容请搜索本站以前的文章或继续浏览下面的相关文章,希望大家以后多多支持本站!