一、默认情况

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的更多相关文章

  1. 23. docker 数据持久化 bind mounting

    1. bind mounting 和 data volume 的区别 data volume 需要在 Dockerfile 定义 Volume bind mounting 只需要 -v 指明 容器外部 ...

  2. Docker Swarm bind 数据持久化

    Docker Swarm bind 数据持久化 bind:主要将工作节点宿主级文件或目录,同步挂载到容器中. 环境: 系统:Centos 7.4 x64 应用版本:Docker 18.09.0 管理节 ...

  3. Docker数据持久化及实战(Nginx+Spring Boot项目+MySQL)

    Docker数据持久化: Volume: (1)创建mysql数据库的container docker run -d --name mysql01 -e MYSQL_ROOT_PASSWORD= my ...

  4. Docker 容器数据 持久化(系统学习Docker05)

    写在前面 本来是可以将数据存储在 容器内部 的.但是存在容器内部,一旦容器被删除掉或者容器毁坏(我亲身经历的痛,当时我们的大数据平台就是运行在docker容器内,有次停电后,不管怎样容器都起不来.以前 ...

  5. docker 系列 - 容器数据持久化和数据共享

    docker 主要有两种数据存储形式, 一种是storage driver(也叫做 Graph driver), 另一种是 volume driver. stroage driver主要是存储那些无状 ...

  6. Docker集群管理Swarm数据持久化

    一.前言 和docker容器一样,Swarm集群中运行的服务也能够做数据持久化.我们可以通过volume.bind和nfs等方式来实现swarm集群应用数据的持久化.其实和docker数据持久化的形式 ...

  7. 【07】循序渐进学 docker:数据持久化

    写在前面的话 学到这里相信有心的朋友都发现问题了,我们每次都会去删掉容器,在创建新的容器.那数据怎么办?岂不删库跑路了? 就算不是数据库,假设公司有日志保留的需求,那每一次发布岂不日志都被干掉了? D ...

  8. Redis进阶:数据持久化,安全,在PHP中使用

    一.redis数据持久化 由于redis是一个内存数据库,如果系统遇到致命问题需要关机或重启,内存中的数据就会丢失,这是生产环境所不能允许的.所以redis提供了数据持久化的能力. redis提供了两 ...

  9. docker数据持久化

    转载/参考: https://www.jianshu.com/p/ef0f24fd0674 Docker的数据持久化主要有两种方式: bind mount docker managed volume ...

随机推荐

  1. bash / powershell切换到脚本所在目录

    切换工作目录到脚本所在目录 bash: #!/usr/bin/env sh cd $(dirname $0) #cd $(dirname $(readlink $0)) #soft link powe ...

  2. RandomAccessFile(),读写文件数据的API,以及复制文件操作

    package seday03;import java.io.File;import java.io.RandomAccessFile; import java.io.IOException; /** ...

  3. 异常处理类-Throwable源码详解

    package java.lang; import java.io.*; /** * * Throwable是所有Error和Exceptiong的父类 * 注意它有四个构造函数: * Throwab ...

  4. 关于 SONY WF1000XM3 在 Windows 10 下蓝牙连接只有 Handfree 没有 Stereo 模式

    应该是驱动适配问题,目前粗暴的解决方案貌似下载安装一个 Intel APTX 驱动就可以了: https://www.dell.com/support/home/cn/zh/cndhs1/driver ...

  5. Dynamics CRM定制子网格添加按钮实例之一

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复221或者20160430可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong. ...

  6. [Flink]测试用的fake温度传感器

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  7. Tomcat下载教程

    首先确定你Windows系统是64位,还是32位(现在大部分是64位) 查看操作系统位数步骤:(WindowsXP,Windows7,Windows8,Windows10查看步骤大同小异,举例Wind ...

  8. STL顺序容器的基本操作

    容器主要分为:顺序容器和关联容器 顺序容器和关联容器,顺序容器主要有:vector.list.deque等.其中vector表示一段连续的内存地址,基于数组的实现,list表示非连续的内存,基于链表实 ...

  9. axios如何先请求A接口然后在请求B接口

    总结:在第一个then的请求结束后,在添加一个then,表示请求第二个接口,在第二个then里面写第二个接口的请求方式 axios.get("./a.json").then(res ...

  10. 数据库 查询第31-40行数据,ID不连续

    一.SQLServer 大致分为两种情况:ID连续和ID不连续. 1.ID连续的情况: select * from A where ID between 31 and 40 2.ID不连续的情况: ( ...