一、默认情况

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. centos 8 docker-ce 安装

    https://www.techrepublic.com/article/a-better-way-to-install-docker-on-centos-8/ https://linuxconfig ...

  2. 数据库之MySQL与Python交互

    准备数据 创建数据表 -- 创建 "京东" 数据库 create database jing_dong charset=utf8; -- 使用 "京东" 数据库 ...

  3. dictionary vs Hashtables

    DictionaryDictionary is generic type Dictionary<TKey,TValue>Dictionary class is a strong type ...

  4. C# 判断(Excel)文件是否已经打开

    using System.IO; using System.Runtime.InteropServices;   [DllImport("kernel32.dll")] publi ...

  5. java基础(21):异常

    1. 异常 什么是异常?Java代码在运行时期发生的问题就是异常. 在Java中,把异常信息封装成了一个类.当出现了问题时,就会创建异常类对象并抛出异常相关的信息(如异常出现的位置.原因等). 1.1 ...

  6. C++ 自增、自减运算符的重载和性能分析

    01 ++.--运算符重载函数的格式 自增运算符和自减运算符是有前置和后置之分的,如: a++ // 后置自增运算符 ++a // 前置自增运算符 b-- // 后置自减运算符 --b // 前置自减 ...

  7. 表单_HTML

    HTML表单_输入元素 大多数情况被用到的表单标签是输入标签 输入类型是由类型属性(type)定义的. 表单中的单选按钮可以设置以下几个属性:value.name.checked value:提交数据 ...

  8. Material 风格的搜索框MaterialSearchView的使用

    大多数App中都有搜索的功能,虽然国内实实在在的遵循Google material design设计语言来设计的App实在不多,但个人感觉MD真的是非常值得研究,这次给大家介绍的是 Material ...

  9. scala 语法速查

    摘自官网 variables   var x = 5 Good x = 6 Variable. val x = 5 Bad x = 6 Constant. var x: Double = 5 Expl ...

  10. linux环境下安装selenium+chrom+chromdriver.exe

    原文:https://blog.csdn.net/yoyocat915/article/details/80580066 原文:https://blog.csdn.net/hanxue6898/art ...