docker容器数据卷

docker容器数据卷

什么是容器数据卷

docker的理念回顾
将应用和环境打包成一个镜像
如果数据都在容器中,那么我们容器删除,数据就会丢失。 需求:数据可以持久化
例:MySQL,容器删了,等于删库跑路。 需求:MySQL数据可以存储在本地。
容器之间可以有一个数据共享的技术,Docker容器中产生的数据,同步到本地。
这就是卷技术–目录的挂载,将我们容器内的目录,挂载到Linux上面。
总结:容器的持久化和同步操作,容器间也可以数据共享。

使用数据卷

方式1:直接使用命令来挂载 -v

docker run -it -v 主机目录:容器内目录
#测试
docker run -it -v /home/test:/home centos /bin/bash

[root@VM-16-11-centos ~]# ls /home/
test
#启动起来后可以通过docker inspect 容器id查看
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/home/test",
                "Destination": "/home",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
#测试文件的同步
#容器内创建文件
[root@54cb866a5e37 home]# touch test1.txt
[root@54cb866a5e37 home]# ls /home/
test1.txt
#宿主机
[root@VM-16-11-centos ~]# ls /home/test/
test1.txt

#停止容器后,在宿主机创建文件
[root@54cb866a5e37 home]# exit
exit
[root@VM-16-11-centos ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND             CREATED         STATUS                          PORTS                                       NAMES
54cb866a5e37   centos         "/bin/bash"         6 minutes ago   Exited (0) About a minute ago                                               heuristic_neum
[root@VM-16-11-centos ~]# ls /home/test/
test1.txt
[root@VM-16-11-centos ~]# touch /home/test/test02.txt
[root@VM-16-11-centos ~]# ls /home/test/
test02.txt  test1.txt
[root@VM-16-11-centos ~]# echo "11111">> /home/test/test1.txt 
[root@VM-16-11-centos ~]# cat /home/test/test1.txt 
11111
#启动容器,在容器中查看
[root@VM-16-11-centos ~]# docker start 54cb866a5e37
54cb866a5e37
[root@VM-16-11-centos ~]# docker attach 54cb866a5e37
[root@54cb866a5e37 /]# ls /home/
test02.txt  test1.txt
[root@54cb866a5e37 /]# cat /home/test1.txt 
11111

优势:只需要在本地修改,容器内自动同步更新

具名和匿名挂载

#匿名挂载
-v 容器内路径
docker run -d -P --name nginx01 -v /etc/nginx nginx

#查看所有的volume的情况
[root@VM-16-11-centos ~]# docker volume ls
DRIVER    VOLUME NAME
local     aa8efab93941dd44a72010d453fa8e3725538476a151b11ec2f2fa5e9e19dedf
local     e353db8eac2b73338f2c43d74254ae4c9ec36af88b9fc823be9c8ce8df6d2632
#匿名挂载在-v后只写了容器内的路径

#具名挂载
[root@VM-16-11-centos ~]# docker run -d -P --name nginx02 -v juming:/etc/nginx nginx
9b04687b526f46f430e4509c9538be4f8a977a11312d9a7b73efa005e5983505
[root@VM-16-11-centos ~]# docker volume ls
DRIVER    VOLUME NAME
local     aa8efab93941dd44a72010d453fa8e3725538476a151b11ec2f2fa5e9e19dedf
local     e353db8eac2b73338f2c43d74254ae4c9ec36af88b9fc823be9c8ce8df6d2632
local     juming
#通过-v 卷名:容器内路径
#查看这个卷
[root@VM-16-11-centos ~]# docker volume inspect juming
[
    {
        "CreatedAt": "2021-05-04T13:44:42+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/juming/_data",
        "Name": "juming",
        "Options": null,
        "Scope": "local"
    }
]

所有的docker容器内的卷,没有指定目录的情况下都是在"/var/lib/docker/volumes/xxxxx/_data"
通过具名挂载可以方便的找到一个卷,这是大多数情况都使用的“具名挂载”。

#如何确定是具名挂载还是匿名挂载,还是指定路径挂载
-v 容器内路径				#匿名挂载
-v 卷名:容器内路径		#具名挂载
-v 宿主机路径:容器内路径	#指定路径挂载

#扩展
#通过 -v容器内路径,ro rw改变读写权限
ro 		#只读
rw		#可读可写

#这个设置了容器权限,容器对挂载出的内容限定了
docker run -d -P --name nginx02 -v juming:/etc/nginx:ro nginx
docker run -d -P --name nginx02 -v juming:/etc/nginx:rw nginx

#ro 说明这个路径只能通过宿主机来操作,容器内无法操作。

#子容器父容器之间的数据共享挂载
--volumes-from
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值