docker-数据存储

Docker持久化数据的方案

  • 基于本地文件系统的Volume。可以在执行Docker create或者Docker run时候,通过-V参数将主机的目录作为容器的数据卷,这部分功能便是基于本地文件系统的Volume管理。

  • 基于Plugin的Volume,支持第三方的存储方案

关于Volume的类型

  • 受管理的datae Volume,由docker后台自动创建

  • 绑定挂载的Volume,具体挂载位置可以由用户来指定位置

Data Volume

使用的场景:某些容器自己会产生数据,例如数据库等,不能将数据删除或者弄丢了所有就有了持久化的用武之地。

1.运行docker实例并且设置跳过密码

Data Volume的语法
        docker run -v 自定义的名称:/保存在真机硬盘的位置
docker run -d -v mysql:/var/lib/mysql --name my_mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql

2.查看docker volume

docker volume ls

3.写入数据再删除容器

docker exec -it my_mysql /bin/bash  #进入容器并且创建新的终端
root@5208bb137409:/#mysql -e "create database cc;"      #创建数据库
root@5208bb137409:/#mysql -e "create table cc.ff(id int,name char(10),age int(5));"     #创建表
exit
docker stop my_mysql
docker rm my_mysql

创建新的数据库登入查看

docker run -d -v mysql:/var/lib/mysql --name mysql_2 -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql       #创建新的数据库名字为mysql_2指定docker volume的地址
docker exec -it mysql_2 /bin/bash       #进入mysql_2的容器
mysql -e "show databases;"      #存在证明我们的持久化成功

Bind Mouting

解释:

语法格式:
        docker run -v 容器中的目录:/宿主机的目录    #容器里面的目录和我们的宿主机的目录同步

实例:

mkdir /nginx
cd /nginx;echo "hello word">index.html
docker run -d -p 80:80 -v $(pwd):/usr/share/nginx/html --name web docker.io/nginx   创建Nginx的容器,端口映射,将当前的目录传递给我们的docker
curl 127.0.0.1
        Hello word      #成功访问并映射到容器中
修改我们的index.htm内容curl一次测试

docker-数据存储》有9个想法

  1. Hi there very nice web site!! Man .. Beautiful .. Amazing .. I will bookmark your website and take the feeds additionally…I’m glad to find numerous useful information here in the publish, we want develop extra techniques on this regard, thank you for sharing.

  2. Hi, Neat post. There is a problem along with your site in web explorer, could check this… IE nonetheless is the market leader and a large component of other people will leave out your fantastic writing because of this problem.

  3. Thanks for the sensible critique. Me and my neighbor were just preparing to do some research on this. We got a grab a book from our area library but I think I learned more from this post. I am very glad to see such fantastic information being shared freely out there.

  4. You could certainly see your enthusiasm in the work you write. The sector hopes for more passionate writers like you who aren’t afraid to say how they believe. Always go after your heart. “In order to preserve your self-respect, it is sometimes necessary to lie and cheat.” by Robert Byrne.

  5. Thanks for any other informative blog. The place else may I am getting that kind of info written in such a perfect means? I have a mission that I am just now operating on, and I have been on the glance out for such information.

  6. Great post. I was checking continuously this blog and I’m impressed!

    Extremely helpful info specially the last part :
    ) I care for such information a lot. I was seeking this particular info
    for a very long time. Thank you and best of luck.

发表评论

电子邮件地址不会被公开。 必填项已用*标注