网站首页 > 精选教程 正文
大家在使用Docker部署web应用或者mysql数据库时,会发现当容器重启后,容器运行过程中产生的日志或者数据库数据都会被清空,那么我们如何保存这些数据呢?这就需要了解docker如何挂载宿主机磁盘目录,用来永久存储数据。
1. 创建容器时执行Docker Volume
使用 docker run 命令,可以运行一个 Docker容器,使用镜像ubuntu/nginx,挂载本地目录/tmp/source到容器目录/tmp/destination
docker run -itd --volume /tmp/source:/tmp/destination --name test ubuntu/nginx bash
基于ubuntu/nginx镜像创建了一个Docker容器。
指定容器的名称为test,由 ––name 选项指定。
Docker Volume 由 ––volume (可以简写为-v)选项指定,主机的 /tmp/source 目录与容器中的 /tmp/destination 目录一一对应。
2. 查看Docker Volume
使用 docker inspect 命令,可以查看 Docker容器 的详细信息:
docker inspect --format='{{json .Mounts}}' test | python -m json.tool [ { "Destination": "/tmp/destination", "Mode": "", "Propagation": "", "RW": true, "Source": "/tmp/source", "Type": "bind" } ]
使用 ––format 选项,可以选择性查看需要的容器信息。 .Mount 为容器的 Docker Volume 信息。
python -m json.tool 可以将输出的json字符串格式化显示。
Source 表示主机上的目录,即 /tmp/source 。
Destination 为容器中的目录,即 /tmp/destination。
3. 本机文件可以同步到容器
在本机/tmp/source目录中新建hello.txt文件
touch /tmp/source/hello.txt ls /tmp/source/ hello.txt
hello.txt文件在容器/tmp/destination/目录中可见
使用 docker exec 命令,可以在容器中执行命令。
docker exec test ls /tmp/destination/ hello.txt
所以在宿主机对目录 /tmp/source/ 的修改,可以同步到容器目录 /tmp/destination/ 中。
4. 容器文件可以同步到宿主机
在容器/tmp/destination目录中新建world.txt文件
docker exec test touch /tmp/destination/world.txt docker exec test ls /tmp/destination/ hello.txt world.txt
world.txt文件在宿主机/tmp/source/目录中可见
ls /tmp/source/ hello.txt world.txt
猜你喜欢
- 2024-10-21 php宝塔搭建实战laravel源码OneDrive多网盘挂载程序
- 2024-10-21 Docker下安装超常用的nginx容器(alpine版)
- 2024-10-21 Docker提交镜像-数据卷-可视化 docker镜像的导入和导出
- 2024-10-21 Nginx 集群负载均衡器 nginx负载均衡策略配置 实例
- 2024-10-21 Nginx-Docker镜像配置详解 docker中的nginx镜像运行后的主要功能是什么
- 2024-10-21 玩转 Linux 之:磁盘分区、挂载知多少?
- 2024-10-21 如何优化一个秒杀项目? 秒杀的实现方案
- 2024-10-21 Docker 学习笔记4 Nginx Php Tomcat Redis Mysql等常用工具
- 2024-10-21 Linux 下挂载新硬盘方法 linux 如何挂载硬盘
- 2024-10-21 轻松掌握Docker数据卷、直接挂载目录到容器镜像
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- nginx反向代理 (57)
- nginx日志 (56)
- nginx限制ip访问 (62)
- mac安装nginx (55)
- java和mysql (59)
- java中final (62)
- win10安装java (72)
- java启动参数 (64)
- java链表反转 (64)
- 字符串反转java (72)
- java逻辑运算符 (59)
- java 请求url (65)
- java信号量 (57)
- java定义枚举 (59)
- java字符串压缩 (56)
- java中的反射 (59)
- java 三维数组 (55)
- java插入排序 (68)
- java线程的状态 (62)
- java异步调用 (55)
- java中的异常处理 (62)
- java锁机制 (54)
- java静态内部类 (55)
- java怎么添加图片 (60)
- java 权限框架 (55)
本文暂时没有评论,来添加一个吧(●'◡'●)