云服务

Docker的持久化存储和数据共享

2019-12-12  本文已影响0人  小波同学

一、基本概述

docker 主要有两种数据存储形式, 一种是storage driver(也叫做 Graph driver), 另一种是 volume driver。 stroage driver主要是存储那些无状态的数据,写入密集型的场景应该使用 volume driver。

1.1 storage deiver

容器运行的文件系统是镜像层和容器层组成的,一层一层叠加,只有最上面的那层是可写的,其他层都是只读的。 Docker通过Union FS技术支持文件的读写和新建,Docker 采用插件式的方式支持多种Union FS实现,官方文档中一般使用stroage driver术语,目前已经有多种实现的插件, 比如: aufs、overlay、overlay2、devicemanger等等。下面介绍几个查看storage-driver的命令:

image.png

Container是在Image之上去创建的,不同于镜像(Image)就是一堆只读层(read-only layer),而Container最上面那一层(Container layer)是可读可写的。

有些容器会自动产生一些数据,为了不让数据随着container的消失而消失,保证数据的安全性。例如:数据库容器,数据表的表会产生一些数据,如果我把container给删除,数据就丢失。为了保证数据不丢失,这就有了Volume的存在。

Data Volume 结构图

image.png

1.2、VOLUME DRIVER(Docker持久化数据的方案)

1.3、Volume的类型

1.4、bind mount和data volume对比

image.png image.png

二、数据持久化--Data Volume

Dockerfile中,通过VOLUME指定容器某一个目录的数据挂载到linux主机的一个目录里,并且会创建一个volume对象。主要有以下两点:

VOLUME ["/var/lib/mysql"]
docker run -v mysql:/var/lib/mysql

2.1 实验:数据持久化

[root@docker01 ~]# docker run -d -e MYSQL_ALLOW_EMPTY_PASSWORD=true --name mysql1 mysql
d2553cdf98e33a286f684f186607be974b275f9183775afb08c0f95ba835d66f
[root@docker01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
d2553cdf98e3        mysql               "docker-entrypoint.s…"   5 seconds ago       Up 3 seconds        3306/tcp, 33060/tcp   mysql1
[root@docker01 ~]# docker volume ls
DRIVER              VOLUME NAME
local               d453f377d82a37ec03a3d6da4f556d84b49d626c7633f352b2746428e8a10952

可以清楚的看到这个volume是挂载到了本机的 /data/docker/volumes/d453f377d82a37ec03a3d6da4f556d84b49d626c7633f352b2746428e8a10952/_data目录

[root@docker01 ~]# docker volume inspect d453f377d82a37ec03a3d6da4f556d84b49d626c7633f352b2746428e8a10952
[
    {
        "CreatedAt": "2019-03-31T12:40:42+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/data/docker/volumes/d453f377d82a37ec03a3d6da4f556d84b49d626c7633f352b2746428e8a10952/_data",
        "Name": "d453f377d82a37ec03a3d6da4f556d84b49d626c7633f352b2746428e8a10952",
        "Options": null,
        "Scope": "local"
    }
]
[root@docker01 ~]# docker run -d -e MYSQL_ALLOW_EMPTY_PASSWORD=true --name mysql2 mysql
22e94df404e7519a80d7cfb26069b6874d3e0a92e71d365ca6ac2ad16f417a35
[root@docker01 ~]# docker volume ls
DRIVER              VOLUME NAME
local               0a49e6a7e653cd4543eb537eba0a7d8e2f9caf20d53af9935183ed1a5dc7f0e4
local               d453f377d82a37ec03a3d6da4f556d84b49d626c7633f352b2746428e8a10952

删除两个容器之后,发现volume还存在本机,说明数据已经持久化保存下来了~

[root@docker01 ~]# docker stop mysql1 mysql2
mysql1
mysql2
[root@docker01 ~]# docker rm  mysql1 mysql2
mysql1
mysql2
[root@docker01 ~]# docker volume ls
DRIVER              VOLUME NAME
local               0a49e6a7e653cd4543eb537eba0a7d8e2f9caf20d53af9935183ed1a5dc7f0e4
local               d453f377d82a37ec03a3d6da4f556d84b49d626c7633f352b2746428e8a10952

通过上面的实验过程,说明在dockerfile中指定VOLUME后,会在本机挂载一个数据目录,保证数据的持久化。但是这种方式也存在着一些不足:

2.2 实验:指定volume name

[root@docker01 ~]# docker volume rm 0a49e6a7e653cd4543eb537eba0a7d8e2f9caf20d53af9935183ed1a5dc7f0e4
0a49e6a7e653cd4543eb537eba0a7d8e2f9caf20d53af9935183ed1a5dc7f0e4
[root@docker01 ~]# docker volume rm d453f377d82a37ec03a3d6da4f556d84b49d626c7633f352b2746428e8a10952
d453f377d82a37ec03a3d6da4f556d84b49d626c7633f352b2746428e8a10952
[root@docker01 ~]# docker volume ls
DRIVER              VOLUME NAME
[root@docker01 ~]# docker run -d -v mysql:/var/lib/mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=true --name mysql1 mysql
8786fddb65691131f645c1ff1d9ed2467c87017505bed0239f2fe7e9ad2a454d
[root@docker01 ~]# docker volume ls
DRIVER              VOLUME NAME
local               mysql
[root@docker01 ~]# docker volume inspect mysql
[
    {
        "CreatedAt": "2019-03-31T12:52:25+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/data/docker/volumes/mysql/_data",
        "Name": "mysql",
        "Options": null,
        "Scope": "local"
    }
]
[root@docker01 ~]# docker exec -it mysql1 /bin/bash
root@8786fddb6569:/# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.15 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> create database test;
Query OK, 1 row affected (0.00 sec)

mysql> quit
Bye
root@8786fddb6569:/# exit
[root@docker01 _data]# docker stop mysql1
docker rmmysql1
[root@docker01 _data]# docker rm mysql1
mysql1
[root@docker01 _data]# docker run -d -v mysql:/var/lib/mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=true --name mysql2 mysql
7729d4cd8f66bf10ecc3e5966e4565f9de8daa11e7d79c33d7a5f27f01152fc5
[root@docker01 _data]# docker exec -it mysql2 /bin/bash
root@7729d4cd8f66:/# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.15 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.01 sec)

mysql> \q
Bye
root@7729d4cd8f66:/# exit
exit

三、数据持久化--Bind Mouting

只需要再运行的时候指定本地文件和容器目录的映射关系即可,不需要再Dockerfile中指定VOLUME:

docker run -v /home/aaa:/root/aaa

3.1 实验:Bind Mouting实验

第一步:创建一个自定义nginx的镜像

#准备首页文件index.html


<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">

  <title>hello</title>

</head>

<body>
  <h1>Hello Docker! </h1>
</body>
</html>

#准备Dockerfile文件


FROM nginx:latest
WORKDIR /usr/share/nginx/html
COPY index.html index.html

#创建镜像
docker build -t my-nginx .
docker run -d -p 80:80 -v $(pwd):/usr/share/nginx/html  --name my-nginx my-nginx 
[root@docker01 docker-nginx]# docker exec -it my-nginx /bin/bash
root@7d5a09fbe98c:/usr/share/nginx/html# ls
Dockerfile  index.html

第四步:在容器当中的/usr/share/nginx/html目录下创建一个test文件,写入内容'aaa'

root@7d5a09fbe98c:/usr/share/nginx/html# echo aaa >> test

第五步:退出容器,并在宿主机目录下查看到test文件

root@7d5a09fbe98c:/usr/share/nginx/html# exit
exit
[root@docker01 docker-nginx]# ls 
Dockerfile  index.html  test
[root@docker01 docker-nginx]# cat test 
aaa

这个实验说明了bind mouting挂载成功~~

参考:
https://www.cnblogs.com/liuguangjiji/p/10630801.html

https://my.oschina.net/665544/blog/1933032

https://www.cnblogs.com/qixidi/p/10229228.html

上一篇 下一篇

猜你喜欢

热点阅读