什么是容器数据卷
需求:我们在容器中运行mysql服务. 怎么做到容器销毁我们的数据不丢失 实现数据的持久化
猜想: 我们要是能够将我们的数据 存储到我们的容器的宿主机上这样我们就不担心我们的数据丢失了(物理故障除外)
技术实现: 数据卷技术就能实现这个需求
使用数据卷
1.直接使用数据卷
docker run -it -v 主机目录:容器目录
[root@localhost ~]# docker run -d -it --name=centos05 -v /root/test:/home/ centos
95c671f317f397e996177282559fea96b86698283c45c5d3806dca7ec5f40194
[root@localhost ~]# docker inspect centos05
测试文件同步:
1、宿主机操作文件容器同步操作
2、容器操作宿主机同步
3.关闭容器测试:
我们能惊奇的发现数据的同步哪怕容器处于关闭的状态也是可以同步的
实战: mysql数据持久化
1.安装MySQL数据库
docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 m
ysql:5.7
Unable to find image 'mysql:5.7' locally
5.7: Pulling from library/mysql
d121f8d1c412: Already exists
f3cebc0b4691: Already exists
1862755a0b37: Already exists
489b44f3dbb4: Already exists
690874f836db: Already exists
baa8be383ffb: Already exists
55356608b4ac: Already exists
277d8f888368: Pull complete
21f2da6feb67: Pull complete
2c98f818bcb9: Pull complete
031b0a770162: Pull complete
Digest: sha256:14fd47ec8724954b63d1a236d2299b8da25c9bbb8eacc739bb88038d82da4919
Status: Downloaded newer image for mysql:5.7
0b80fee1e472fa46f234cc02c04a7bf62f8466bcecb219c4bc1ef4ee5c8acb33
2.查看我们宿主机目录 看看是否有我们的mysql数据
[root@localhost mysql]# pwd
/home/mysql
[root@localhost mysql]# ls
conf data
[root@localhost mysql]# cd data/
[root@localhost data]# ls
auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sys
ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem
[root@localhost data]#
3.删除docker 容器
[root@localhost data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0b80fee1e472 mysql:5.7 "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 33060/tcp, 0.0.0.0:3310->3306/tcp mysql01
[root@localhost data]# docker rm -f mysql01
mysql01
[root@localhost data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4.我们的数据文件是否还在
[root@localhost data]# ls
auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sys
ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem test
5.我们的持久化存储实现
那么想这样的挂载有几种呢?
匿名挂载和具名挂载
# 匿名挂载
-v 容器内路径
root@localhost ~]# docker run -d -it --name=Nginx01 -v /etc/nginx nginx
[root@localhost ~]# docker inspect Nginx01
....
"Mounts": [
{
"Type": "volume",
"Name": "0eef256e6a4c6ef5e0b2e5376294e25d498b5caba98733f7ce5641add15bdfb5",
"Source": "/var/lib/docker/volumes/0eef256e6a4c6ef5e0b2e5376294e25d498b5caba98733f7ce5641add15bdfb5/_data",
"Destination": "/etc/nginx",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
.....
# 查看所有卷的情况
[root@localhost ~]# docker volume ls
DRIVER VOLUME NAME
local 0eef256e6a4c6ef5e0b2e5376294e25d498b5caba98733f7ce5641add15bdfb5
local 00d9a09b3f3bf62352e563ee6fbd191c13af9939d58974f11c2d98a1cc832f99
local 25702591833c4618d78e2fc32598437a1c388f787210846d14ceb6eb5ab88cb7
#具名挂载
-v 挂载卷名:容器内路径
[root@localhost ~]# docker run -d -it --name=Nginx02 -P -v Nginx-conf:/etc/nginx nginx
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a87e394c3794 nginx "/docker-entrypoint.…" 5 seconds ago Up 4 seconds 0.0.0.0:32768->80/tcp Nginx02
fbfb104587c9 nginx "/docker-entrypoint.…" 9 minutes ago Up 9 minutes 80/tcp Nginx01
[root@localhost ~]# docker volume ls
DRIVER VOLUME NAME
local 0eef256e6a4c6ef5e0b2e5376294e25d498b5caba98733f7ce5641add15bdfb5
local 00d9a09b3f3bf62352e563ee6fbd191c13af9939d58974f11c2d98a1cc832f99
local 25702591833c4618d78e2fc32598437a1c388f787210846d14ceb6eb5ab88cb7
local Nginx-conf
#查看数据卷详细信息
[root@localhost ~]# docker volume inspect Nginx-conf
[
{
"CreatedAt": "2020-09-10T12:46:44+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/Nginx-conf/_data",
"Name": "Nginx-conf",
"Options": null,
"Scope": "local"
}
]
#加上前面说的指定目录挂载 总共三种方法
挂载卷是否有权限限制?有哪几种?
拓展:
# 默认是 rw
# ro 只要看到ro就说明这个路径只能通过宿主机来操作,容器内部是无法操作!
# 通过 -v 容器内路径:ro 或 rw 改变读写权限
# 一旦创建容器时设置了容器权限,容器对我们挂载出来的内容就有限定了!
docker run -d -P --name nginx05 -v Nginx-conf:/etc/nginx:ro nginx
docker run -d -P --name nginx05 -v Nginx-conf:/etc/nginx:rw nginx
数据卷之dockerFIle
初识DockerFile
什么是dockerfile?
Dockerfile
是一个文本文档,其中包含用户可以在命令行上调用以组合图像的所有命令。使用docker build
用户可以创建一个自动执行的构建,该构建可以连续执行多个命令行指令。
Dockerfile 就是用来构建 docker镜像的构建文件!命令脚本! 先体验一下!
通过这个脚本可以生成镜像,镜像是一层一层的,脚本是一个个的命令,每个命令都是最终镜像的一层!
实战:dockerfile匿名挂载数据卷
#1.创建dockerfile文件
[root@localhost volume]# cat dockerfile
FROM centos
VOLUME ["volume01","volume02"]
CMD echo"-----------build secccessful!---------"
CMD /bin/bash
#2.build dockerfile
[root@localhost volume]# docker build -f dockerfile -t martin/centos-volume .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM centos
---> 0d120b6ccaa8
Step 2/4 : VOLUME ["volume01","volume02"]
---> Running in 08bd38fc2b20
Removing intermediate container 08bd38fc2b20
---> 5373c024b9bd
Step 3/4 : CMD echo"-----------build secccessful!---------"
---> Running in 47e69c382a92
Removing intermediate container 47e69c382a92
---> 5294966f9a51
Step 4/4 : CMD /bin/bash
---> Running in 6b63ec9c5d91
Removing intermediate container 6b63ec9c5d91
---> 0080b2fa0c13
Successfully built 0080b2fa0c13
Successfully tagged martin/centos-volume:latest
#3查看镜像
[root@localhost volume]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
martin/centos-volume latest 0080b2fa0c13 18 seconds ago 215MB
martin/tomcat02 0.1 75448ab28707 2 hours ago 652MB
#4以新生成的镜像创建容器
[root@localhost volume]# docker run -d -it --name=test-dof-vol martin/centos-volume
38452f2db864f1c0136cb63021af8b66ac1dfae29ab0c451479b85840a6f7637
[root@localhost volume]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
38452f2db864 martin/centos-volume "/bin/sh -c /bin/bash" 8 seconds ago Up 7 seconds test-dof-vol
5.查看和测试数据卷
[root@localhost volume]# docker inspect test-dof-vol
"Mounts": [
{
"Type": "volume",
"Name": "d577c8ff3c360963159e3631d2c307a1a44ce068c2888b167b035e527bb4005b",
"Source": "/var/lib/docker/volumes/d577c8ff3c360963159e3631d2c307a1a44ce068c2888b167b035e527bb4005b/_data",
"Destination": "volume01",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "0745d3159fe5295f1262c872a487614c563147aadcb2ec519f449817f3804521",
"Source": "/var/lib/docker/volumes/0745d3159fe5295f1262c872a487614c563147aadcb2ec519f449817f3804521/_data",
"Destination": "volume02",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
测试:
数据卷容器
需求: 多个容器同步数据
#数据卷容器
在启动容器是 使用 --volumes-from 容器id|容器名 #会将容器id的数据卷作为数据容器挂载到新的容
#启动容docker01 作为容器卷
[root@localhost _data]# docker run -d -it --name=docker01 martin/centos-volume
[root@localhost _data]# docker inspect docker01
"Mounts": [
{
"Type": "volume",
"Name": "d5c127e77ae816374681bab51f1319e4935d593d20f6a94f7267473727786fc9",
"Source": "/var/lib/docker/volumes/d5c127e77ae816374681bab51f1319e4935d593d20f6a94f7267473727786fc9/_data",
"Destination": "volume01",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "4e64fa0f86a8080c25a92829f70f1350b6c6a7ac75cd0d877b14ed04ac6d9dbd",
"Source": "/var/lib/docker/volumes/4e64fa0f86a8080c25a92829f70f1350b6c6a7ac75cd0d877b14ed04ac6d9dbd/_data",
"Destination": "volume02",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
]
#启动dcoker02 以docker01 为容器卷启动
[root@localhost _data]# docker run -d -it --name=docker02 --volumes-from docker01 centos
a4006a8881b55527db8fb50dec272dbd5670b923a70a9eadc951300a003fce77
[root@localhost _data]# docker inspect docker02
"Mounts": [
{
"Type": "volume",
"Name": "d5c127e77ae816374681bab51f1319e4935d593d20f6a94f7267473727786fc9",
"Source": "/var/lib/docker/volumes/d5c127e77ae816374681bab51f1319e4935d593d20f6a94f7267473727786fc9/_data",
"Destination": "volume01",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "4e64fa0f86a8080c25a92829f70f1350b6c6a7ac75cd0d877b14ed04ac6d9dbd",
"Source": "/var/lib/docker/volumes/4e64fa0f86a8080c25a92829f70f1350b6c6a7ac75cd0d877b14ed04ac6d9dbd/_data",
"Destination": "volume02",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
]
#我们发现容器docker02 数据卷是用共享了docker01的数据卷
#以docker01为数据容器启动docker03
[root@localhost _data]# docker run -d -it --name=docker03 --volumes-from docker01 centos
9bdb00a12f91300d841f2c26d621f2126c509887af4c9554beaefe60c8062123
docker03 也是共享了docker01的数据卷 此处不板书了 接下来我们测试
测试
1.在数据卷中创建和删除文件看是否同步
2.分别在容器中创建和删除文件文件看是否同步
3.删除docker01 看数据卷是否还能生效
结论 容器只是一种复制容器卷的卷的挂载关系 只要数据存在 我们的映射关系就不会失效