容器的持久化和同步操作!容器间共享数据
两种方式: 命令挂载 -v, 通过 Dockerfile
(方式1)
docker run -it -v 主机目录:容器内目录
shell[root@hecs-152658 ~]# cd test
[root@hecs-152658 test]# pwd
/root/test
# 挂载目录启动 centos
[root@hecs-152658 test]# docker run -it -v /root/test:/home centos /bin/bash
# 容器目录创建 test.txt
[root@d72912bf8cbf /]# cd /home/
[root@d72912bf8cbf home]# touch test.txt
# 宿主机能看到容器创建的内容
[root@d72912bf8cbf home]#
[root@hecs-152658 test]# ls
test.txt
# 查看元信息,有挂载信息说明
[root@hecs-152658 test]# docker inspect d72912bf8cbf
...
"Mounts": [
{
"Type": "bind",
"Source": "/root/test",
"Destination": "/home",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
]
...
# 宿主机添加文件,也能在容器里看到
[root@hecs-152658 test]# touch test01.txt
[root@hecs-152658 test]# docker exec -it d72912bf8cbf /bin/bash
[root@d72912bf8cbf /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@d72912bf8cbf /]# cd home/
[root@d72912bf8cbf home]# ls
test.txt test01.txt
shell#docker run --name mysql -v /root/test/mysql/conf:/etc/mysql/conf.d -v /root/test/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql
# 挂载目录 /etc/mysql/conf.d /var/lib/mysql
[root@hecs-152658 test]# docker run --name mysql -v /root/test/mysql/conf:/etc/mysql/conf.d -v /root/test/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d -p 3310:3306 mysql
Unable to find image 'mysql:latest' locally
latest: Pulling from library/mysql
45b42c59be33: Already exists
b4f790bd91da: Pull complete
325ae51788e9: Pull complete
adcb9439d751: Pull complete
174c7fe16c78: Pull complete
698058ef136c: Pull complete
4690143a669e: Pull complete
f7599a246fd6: Pull complete
35a55bf0c196: Pull complete
790ac54f4c47: Pull complete
b0ddd5d1b543: Pull complete
1aefd67cb33d: Pull complete
Digest: sha256:03306a1f248727ec979f61424c5fb5150e2c5fd2436f2561c5259b1258d6063c
Status: Downloaded newer image for mysql:latest
0421bed2a4ffbeb143b28975caae5048fe863aa900f6f6f20461eccd7d5afdb6
具名挂载: -v 卷名:容器内路径
/
开头匿名挂载: -v 容器内路径
docker volume ls
查看所有卷信息
shell# 这里会发现有些 没有具体名字,这种就是匿名挂载的
# 这里如果是指定宿主机路径挂载,是不会有显示的
[root@hecs-152658 ~]# docker volume ls
DRIVER VOLUME NAME
local 3bbe0bdd591a800f554204fab39d11687440c52539afe8d97518a59a3202e10b
local 8d1377b556b23d859752233540714ecc9ed14cfc0a63118cff67989bd5b61029
local 52a4f91013a8c4951fcb90634b775738d81cb65dffa8e4ea038ca27f2e89ac1a
local 60b225b03df8cf00c332f559b69ebb15eeeaf99951f3f298e167857e7458a37a
local 272c194d3ed2a2e1442651a2fd1ec5f655ab6f4ce5a1fa069b6980ec40c199ce
local 9055ac06cde97b7da7c924208568bae0b7580707233228c4cdb14dac3c19fd6e
local 75477603522e8ccdcb0d1af46529e15778e1e57b4c436b67196273283b9062c2
local cafa7dd831f26a0042a3a4ee42be421a1e9eb86354ee2591bdaf9ec789187549
local f3a29621095d8bcad331eea9661965cf91eb2a49819018e0db57e22a54910728
local portainer_data
local rocket_mongodb_data
# 查看详细地址
[root@hecs-152658 ~]# docker volume inspect portainer_data
[
{
"CreatedAt": "2021-07-29T11:45:52+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/portainer_data/_data",
"Name": "portainer_data",
"Options": null,
"Scope": "local"
}
]
[root@hecs-152658 ~]# docker volume inspect 3bbe0bdd591a800f554204fab39d11687440c52539afe8d97518a59a3202e10b
[
{
"CreatedAt": "2021-07-20T15:35:15+08:00",
"Driver": "local",
"Labels": {
"com.docker.volume.anonymous": ""
},
"Mountpoint": "/var/lib/docker/volumes/3bbe0bdd591a800f554204fab39d11687440c52539afe8d97518a59a3202e10b/_data",
"Name": "3bbe0bdd591a800f554204fab39d11687440c52539afe8d97518a59a3202e10b",
"Options": null,
"Scope": "local"
}
]
所有 docker 容器内部的卷,没有指定目录的情况下都是在 /var/lib/docker/volumes/xxx
下
-v 容器内路径 #匿名挂载
-v 卷名:容器内路径 #具名挂载
-v /宿主机路径:容器内路径 #指定路径挂载
docker run -v path:path
rw
通过 -v 容器内路径: ro 或 rw 改变读写权限
(方式2)
Dockerfile 就是用来构建 docker 镜像的构建文件
shell# 位置
[root@hecs-152658 test-volume]# pwd
/root/test/test-volume
# 创建编辑dockerfile
[root@hecs-152658 test-volume]# vim dockerfile01
dockerfile# dockerfile 文件内容 FROM centos # 指定挂载卷 VOLUME ["volume01", "volume02"] CMD echo "----end----" CMD /bin/bash
shell# build 构建镜像
# -f 指定 dockerfile 文件
[root@hecs-152658 test-volume]# docker build -f ./dockerfile01 -t my-centos .
[+] Building 0.2s (5/5) FINISHED docker:default
=> [internal] load build definition from dockerfile 0.1s
=> => transferring dockerfile: 118B 0.0s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:latest 0.0s
=> [1/1] FROM docker.io/library/centos 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:bcb7b9d022595d87e4f374a55119c8e2e4933dd784edc 0.0s
=> => naming to docker.io/library/my-centos 0.0s
# 查看
[root@hecs-152658 test-volume]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-centos latest bcb7b9d02259 22 months ago 231MB
# 查看挂载信息
[root@hecs-152658 test-volume]# docker run -it my-centos /bin/bash
[root@347f16a1722c /]# ls
bin etc lib lost+found mnt proc run srv tmp var volume02
dev home lib64 media opt root sbin sys usr volume01
# 创建文件看看
[root@347f16a1722c /]# cd volume01
[root@347f16a1722c volume01]# touch test02.txt
# 退出容器,查看元数据
[root@hecs-152658 test-volume]# docker inspect 347f16a1722c
...
"Mounts": [
{
"Type": "volume",
"Name": "678b1afd11e601840f58d69cf1309b359c9625d8e35b198e597013b0005c493c",
"Source": "/var/lib/docker/volumes/678b1afd11e601840f58d69cf1309b359c9625d8e35b198e597013b0005c493c/_data",
"Destination": "volume01",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "f50438d9da2b94645bb987079858c76f2a98a5ebc51c610313151e79dc1a04a8",
"Source": "/var/lib/docker/volumes/f50438d9da2b94645bb987079858c76f2a98a5ebc51c610313151e79dc1a04a8/_data",
"Destination": "volume02",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
# 进入目录看看
[root@hecs-152658 test-volume]# cd /var/lib/docker/volumes/678b1afd11e601840f58d69cf1309b359c9625d8e35b198e597013b0005c493c/_data
[root@hecs-152658 _data]# ls
test02.txt
这种方式未来用得比较多,因为我们通常都会自己构建镜像
多个 mysql 同步数据。
数据卷-> 实现多个容器之间数据共享
shell# 启动一个 centos
[root@hecs-152658 ~]# docker run -it --name centos01 my-centos /bin/bash
[root@be3bb5579fcd /]# [root@hecs-152658 ~]#
[root@hecs-152658 ~]#
# 启动第二个
# --volumes-from 卷继承自 centos01 容器
[root@hecs-152658 ~]# docker run -it --name centos02 --volumes-from centos01 my-centos /bin/bash
[root@2907eb3d3975 /]# [root@hecs-152658 ~]#
[root@hecs-152658 ~]#
# 查看容器
[root@hecs-152658 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2907eb3d3975 my-centos "/bin/bash" 6 seconds ago Up 5 seconds centos02
be3bb5579fcd my-centos "/bin/bash" About a minute ago Up About a minute centos01
# 进入 centos01 创建文件
[root@hecs-152658 ~]# docker attach be3bb5579fcd
[root@be3bb5579fcd /]# cd volume01
[root@be3bb5579fcd volume01]# touch test03.txt
[root@be3bb5579fcd volume01]# read escape sequence
# 进入 centos02 查看是否同步
[root@hecs-152658 ~]# docker attach 2907eb3d3975
[root@2907eb3d3975 /]# ls volume01
test03.txt
# 这个 centos02 就是 数据卷容器
# 这时候删除 centos01 容器,文件还是存在的,centos02 还是可以访问和创建
本文作者:Yui_HTT
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
预览: