Docker容器数据持久化存储机制
一、Docker容器数据持久化存储介绍
-
物理机或虚拟机数据持久化存储
- 由于物理机或虚拟机本身就拥有大容量的磁盘,所以可以直接把数据存储在物理机或虚拟机本地文件系统中,亦或者也可以通过使用额外的存储系统(NFS、GlusterFS、Ceph等)来完成数据持久化存储。
-
Docker容器数据持久化存储
- 由于Docker容器是由容器镜像生成的,所以一般容器镜像中包含什么文件或目录,在容器启动后,我们依旧可以看到相同的文件或目录。
- 由于Docker容器属于“用后即焚”型计算资源,因此Docker容器不适合做数据持久化存储
二、Docker容器数据持久化存储方式
Docker提供三种方式将数据从宿主机挂载到容器中:
- docker run -v
- volumes
- Docker管理宿主机文件系统的一部分(/var/lib/docker/volumes)
- 是Docker默认存储数据方式
- bind mounts
三、Docker容器数据持久化存储方式应用案例演示
3.1 docker run -v
3.1.1 未挂载本地目录
| |
| CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES |
| c4ad9f2c15fa nginx:latest "/docker-entrypoint.…" 46 seconds ago Up 44 seconds 80/tcp web1 |
| 使用curl命令访问容器 |
| |
| <!DOCTYPE html> |
| <html> |
| <head> |
| <title>Welcome to nginx!</title> |
| <style> |
| html { color-scheme: light dark; } |
| body { width: 35em; margin: 0 auto; |
| font-family: Tahoma, Verdana, Arial, sans-serif; } |
| </style> |
| </head> |
| <body> |
| <h1>Welcome to nginx!</h1> |
| <p>If you see this page, the nginx web server is successfully installed and |
| working. Further configuration is required.</p> |
| |
| <p>For online documentation and support please refer to |
| <a href="http://nginx.org/">nginx.org</a>.<br/> |
| Commercial support is available at |
| <a href="http://nginx.com/">nginx.com</a>.</p> |
| |
| <p><em>Thank you for using nginx.</em></p> |
| </body> |
| </html> |
| 查看容器中/usr/share/nginx/html目录中目录或子目录 |
| |
| 50x.html |
| index.html |
3.1.2 挂载本地目录
| 运行web2容器,把/opt/wwwroot目录挂载到/usr/share/nginx/html目录中 |
| |
| 查看容器IP地址 |
| |
| |
| ...... |
| "IPAddress": "172.17.0.3", |
| ...... |
3.1.3 未创建本地目录
| 运行web3容器,挂载未创建的本地目录,启动容器时将自动创建本地目录 |
| |
| 往自动创建的目录中添加一个index.html文件 |
| |
3.2 volumes
3.2.1 创建数据卷
| 创建一个名称为nginx-vol的数据卷 |
| |
| nginx-vol |
| 确认数据卷创建后的位置 |
| |
| backingFsBlockDev metadata.db nginx-vol |
| 查看已经创建数据卷 |
| |
| DRIVER VOLUME NAME |
| local nginx-vol |
| 查看数据卷详细信息 |
| |
| [ |
| { |
| "CreatedAt": "2022-02-08T14:36:16+08:00", |
| "Driver": "local", |
| "Labels": {}, |
| "Mountpoint": "/var/lib/docker/volumes/nginx-vol/_data", |
| "Name": "nginx-vol", |
| "Options": {}, |
| "Scope": "local" |
| } |
| ] |
3.2.2 使用数据卷
| 运行web4容器,使用--mount选项,实现数据卷挂载 |
| |
或
| 查看容器运行后数据卷中文件或子目录 |
| |
| 50x.html index.html |
| 使用curl命令访问容器 |
| |
| <!DOCTYPE html> |
| <html> |
| <head> |
| <title>Welcome to nginx!</title> |
| <style> |
| html { color-scheme: light dark; } |
| body { width: 35em; margin: 0 auto; |
| font-family: Tahoma, Verdana, Arial, sans-serif; } |
| </style> |
| </head> |
| <body> |
| <h1>Welcome to nginx!</h1> |
| <p>If you see this page, the nginx web server is successfully installed and |
| working. Further configuration is required.</p> |
| |
| <p>For online documentation and support please refer to |
| <a href="http://nginx.org/">nginx.org</a>.<br/> |
| Commercial support is available at |
| <a href="http://nginx.com/">nginx.com</a>.</p> |
| |
| <p><em>Thank you for using nginx.</em></p> |
| </body> |
| </html> |
3.3 bind mounts
| 运行web5容器并使用bind mount方法实现本地任意目录挂载 |
| |
| 添加内容至/opt/web5root/index.html中 |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!