数据持久化之bind Mounting
一、默认情况
1.创建一个Nginx测试镜像
Dockerfile:
FROM nginx:latest
WORKDIR /usr/share/nginx/html
COPY index.html index.html #使用当前目录下index.html文件
index.html
[root@localhost docker-nginx]# cat index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>hello</title>
</head>
<body>
<h1>Hello Docker! </h1>
</body>
</html>
编译
[root@localhost docker-nginx]# docker build -t my_nginx:v1 .
[root@localhost docker-nginx]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
my_nginx v1 897d33067e21 seconds ago MB
2.使用my_ingx镜像创建一个容器,并访问
[root@localhost docker-nginx]# docker run -d --name my_nginx -p : my_nginx:v1
2ff4ee3a55e500785b229527fc72a17b34b2ed96c7c9b419a23ab57644aada1d
3.进入容器内部修改index.html,并访问查看结果
[root@localhost docker-nginx]# docker exec -it my_nginx /bin/bash
root@2ff4ee3a55e5:/usr/share/nginx/html# sed -i "s#Hello Docker#Hello Nginx#g" index.html
4.修改当前目录下index.html并访问查看结果
[root@localhost docker-nginx]# sed -i "s#Hello Docker#Hello Nginx on physics#g" index.html
二、使用bind Mounting
1.删除旧容器再新建一个容器,并访问,注意结果
[root@localhost docker-nginx]# docker run -d --name my_nginx -v $(pwd):/usr/share/nginx/html -p : my_nginx:v1
24e94344b29c333fceec7f6164ac5757d4747fd4e71e9a6d45e373dfaf130013
2.进入容器内部修改index.html,并访问查看结果
[root@localhost docker-nginx]# docker exec -it my_nginx /bin/bash
root@24e94344b29c:/usr/share/nginx/html# sed -i "s#Hello Nginx on physics#Hello Nginx on docker#g" index.html
3.退出容器并修改当前目录下index.html,并访问
[root@localhost docker-nginx]# sed -i "s#Hello Nginx on docker#Hello Nginx, This is test page#g" index.html
4.查看本地index.html和容器index.html
[root@localhost docker-nginx]# md5sum index.html #查看宿主机与容器中index.html hash值
60cbc4142a556d3e5a1e812edf288757 index.html
[root@localhost docker-nginx]# docker exec -it my_nginx md5sum index.html
60cbc4142a556d3e5a1e812edf288757 index.html
[root@localhost docker-nginx]# cat index.html #查看宿主机与容器中index.html 内容
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"> <title>hello</title> </head> <body>
<h1>Hello Nginx, This is test page! </h1>
</body>
</html>
[root@localhost docker-nginx]# docker exec -it my_nginx cat index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"> <title>hello</title> </head> <body>
<h1>Hello Nginx, This is test page! </h1>
</body>
</html>
总结及与Data Volume的区别:
.Data Volume 需要在 Dockerfile 内声明需要创建的 volume 目录。
Bind Mounting 则不需要在 Dockerfile 声明 volume,只需要在创建容器的时候,也就是 run 的时候声明即可。 .使用Data Volume持久化的方式,是因为我们容器是一个数据源的产生地方,本身会产生文件和数据,而我们不想让我们的文件和数据随着容器的消失而消失,因此用这种方式持久化
使用Bind Mounting 持久化的方式,则本地的目录文件和容器中的文件是同步的,如果本地的文件做了修改,那么容器中的文件也会修改。
即:Bind mount会覆盖容器中的文件,而volume mount则不会,即如果容器中已有文件,则会将文件同步到主机的目录上
数据持久化之bind Mounting的更多相关文章
- 23. docker 数据持久化 bind mounting
1. bind mounting 和 data volume 的区别 data volume 需要在 Dockerfile 定义 Volume bind mounting 只需要 -v 指明 容器外部 ...
- Docker Swarm bind 数据持久化
Docker Swarm bind 数据持久化 bind:主要将工作节点宿主级文件或目录,同步挂载到容器中. 环境: 系统:Centos 7.4 x64 应用版本:Docker 18.09.0 管理节 ...
- Docker数据持久化及实战(Nginx+Spring Boot项目+MySQL)
Docker数据持久化: Volume: (1)创建mysql数据库的container docker run -d --name mysql01 -e MYSQL_ROOT_PASSWORD= my ...
- Docker 容器数据 持久化(系统学习Docker05)
写在前面 本来是可以将数据存储在 容器内部 的.但是存在容器内部,一旦容器被删除掉或者容器毁坏(我亲身经历的痛,当时我们的大数据平台就是运行在docker容器内,有次停电后,不管怎样容器都起不来.以前 ...
- docker 系列 - 容器数据持久化和数据共享
docker 主要有两种数据存储形式, 一种是storage driver(也叫做 Graph driver), 另一种是 volume driver. stroage driver主要是存储那些无状 ...
- Docker集群管理Swarm数据持久化
一.前言 和docker容器一样,Swarm集群中运行的服务也能够做数据持久化.我们可以通过volume.bind和nfs等方式来实现swarm集群应用数据的持久化.其实和docker数据持久化的形式 ...
- 【07】循序渐进学 docker:数据持久化
写在前面的话 学到这里相信有心的朋友都发现问题了,我们每次都会去删掉容器,在创建新的容器.那数据怎么办?岂不删库跑路了? 就算不是数据库,假设公司有日志保留的需求,那每一次发布岂不日志都被干掉了? D ...
- Redis进阶:数据持久化,安全,在PHP中使用
一.redis数据持久化 由于redis是一个内存数据库,如果系统遇到致命问题需要关机或重启,内存中的数据就会丢失,这是生产环境所不能允许的.所以redis提供了数据持久化的能力. redis提供了两 ...
- docker数据持久化
转载/参考: https://www.jianshu.com/p/ef0f24fd0674 Docker的数据持久化主要有两种方式: bind mount docker managed volume ...
随机推荐
- 2018-8-10-win10-uwp-商业游戏-
原文:2018-8-10-win10-uwp-商业游戏- title author date CreateTime categories win10 uwp 商业游戏 lindexi 2018-08- ...
- .net core使用NLog日志
前言:NLog日志对.net core web项目最新的支持在官网上有最新的介绍: 官网介绍地址:https://github.com/NLog/NLog/wiki/Getting-started-w ...
- 【随笔】CLR:向头对象(Object Header)迈进一大步!!!
前言 在我之前一篇随笔里(戳我),我们知道,一个引用类型的对象,包含了2个额外的开销,一个是头对象(object header),一个是MT.我们接下来看看头对象到底有多神秘... Object He ...
- 最近的项目系之3——core3.0整合Senparc
1.前言 既然是.net下微信开发,自然少不了Senparc,可以说这个框架的存在, 至少节省了微信相关工作量的80%.事实上,项目开始前,还纠结了下是Java还是core,之所以最终选择core,除 ...
- Apache配置https
Apache配置https 之前一直用的是Tomcat,今天突然接到任务要给Apache配置https证书,因为小程序要用.下面把过程列出来以备后续查看. 1.首先你得有ssl证书,没有的可以去购买, ...
- [20190823]关于CPU成本计算3.txt
[20190823]关于CPU成本计算3.txt --//前几天探究CPU cost时遇到的问题,获取行成本时我的测试查询结果出现跳跃,不知道为什么,感觉有点奇怪,分析看看.--//ITPUB原始链接 ...
- [PHP] 现代化PHP之路:composer的安装和升级
1.下载一个脚本文件 wget https://getcomposer.org/installer 2.php执行下这个php脚本 php installer 3.把下载的文件转移到一个PATH环境变 ...
- STL关联容器的基本操作
关联容器 map,set map map是一种关联式容器包含 键/值 key/value 相当于python中的字典不允许有重复的keymap 无重复,有序 Map是STL的一个关联容器,它提供一对一 ...
- java.sql.Date赋值给了java.util.Date.转化成JSONArray时出错net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
net.sf.json.JSONException: java.lang.reflect.InvocationTargetExceptionat net.sf.json.JSONObject.defa ...
- Oracle SQL日期及日期格式获取命令
日期及日期格式: 获取系统日期: sysdate() 格式化日期 to_char(sysdate(),'yyyy-mm-dd,hh24:mi:ss') to_date(sysdate(),'yyyy- ...