本文最后更新于:2021年10月2日 晚上
Docker 容器数据卷 什么是容器数据卷 将应用和环境打包成一个镜像,如果数据都在容器中,删除容器,数据就会丢失。容器之间可以有一个数据共享的技术,使 Docker 容器中产生的数据,同步到本地。这就是卷技术,目录的挂载,将容器内的目录,挂载到 Linux 上面。
容器的持久化和同步操作,容器间也是可以数据共享的
使用数据卷 可直接使用命令 -v 来挂载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 docker run -it -v 主机目录:容器目录 [root@bogon home]# docker run -it -d -v /home/test:/home centos [root@bogon home]# ls chziyue dockerfile gitea test [root@bogon home]# docker inspect 6930ea3963d8 "Mounts" : [ { "Type" : "bind" , "Source" : "/home/test" , "Destination" : "/home" , "Mode" : "" , "RW" : true , "Propagation" : "rprivate" }
进入容器内 home 目录新建一个 index.html 文件
1 2 3 4 [root@bogon home]# docker attach 6930ea3963d8 [root@6930 ea3963d8 /]# cd /home/ [root@6930 ea3963d8 home]# ls [root@6930 ea3963d8 home]# touch index.html
在主机的 home/test 目录下查看,发现在容器内新建的 index.html 文件已经有了
1 2 3 [root@bogon home]# cd /home/test/ [root@bogon test]# ls index.html
停止容器,在主机的 home/test 目录下新建一个 index.php 文件
1 2 [root@bogon test]# docker stop 6930ea3963d8 [root@bogon test]# touch index.php
启动容器,在容器内 home 目录下查看,发现容器也会自动同步到这个文件
1 2 3 4 5 6 [root@bogon test]# docker start 6930ea3963d8 6930 ea3963d8 [root@bogon test]# docker attach 6930ea3963d8 [root@6930 ea3963d8 /]# cd /home/ [root@6930 ea3963d8 home]# ls index.html
如果将容器删除
1 2 3 4 5 6 [root@bogon test]# docker stop 6930ea3963d8 6930 ea3963d8 [root@bogon test]# docker rm -f 6930ea3963d8 6930 ea3963d8 [root@bogon test]# ls index.html index.php
发现,挂载到本地的数据卷依旧没有丢失,里面的文件都还在,这就实现了容器数据持久化功能!
匿名挂载和具名挂载 匿名挂载
-v 后面只写容器内路径
1 2 [root@bogon test]# docker run -d -P --name nginx01 -v /etc/nginx nginx bcca30117a31c7501fe72c4b8919af6381d5cb3fa62a1f3b103a38350e20b3e3
查看 docker volume --help
帮助信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@bogon test]# docker volume --help Usage: docker volume COMMAND Manage volumes Commands: create Create a volume inspect Display detailed information on one or more volumes ls List volumes prune Remove all unused local volumes rm Remove one or more volumes Run 'docker volume COMMAND --help' for more information on a command.
查看所有卷
1 2 3 4 [root@bogon test]# docker volume ls DRIVER VOLUME NAME local 81 da19700cdd76d6020971290842c61662cc73c1f6445b8a4a6e67224e36352d
具名挂载
通过 -v 卷名:容器内路径来实现具名挂载
在启动一个 nginx 使用具名挂载
1 2 3 4 5 6 [root@bogon test]# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx 0533 a600af76366da29b8116168aa506bc9297b8270beedda15ab9e79c0179f1 [root@bogon test]# docker volume ls DRIVER VOLUME NAME local 81 da19700cdd76d6020971290842c61662cc73c1f6445b8a4a6e67224e36352d local juming-nginx
使用 docker inspect
查看挂载的位置
1 2 3 4 5 6 7 8 9 10 11 12 [root@bogon test]# docker inspect juming-nginx [ { "CreatedAt" : "2021-09-15T22:19:34+08:00" , "Driver" : "local" , "Labels" : null, "Mountpoint" : "/var/lib/docker/volumes/juming-nginx/_data" , "Name" : "juming-nginx" , "Options" : null, "Scope" : "local" } ]
所有 docker 容器内的卷,如果没有指定主机目录,都会挂载到 /var/lib/docker/volumes/
目录下
进入到 /var/lib/docker/volumes/
目录下看一看
1 2 3 [root@bogon test]# cd /var/lib/docker/volumes/ [root@bogon volumes]# ls 81 da19700cdd76d6020971290842c61662cc73c1f6445b8a4a6e67224e36352d backingFsBlockDev juming-nginx metadata.db
进入到 juming-nginx
目录下
1 2 3 4 5 6 7 [root@bogon volumes]# cd juming-nginx/ [root@bogon juming-nginx]# ls _data [root@bogon juming-nginx]# cd _data/ [root@bogon _data]# ls conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
通过具名挂载可以方便的找到我们挂载的卷,大多数情况下都会使用这种方式
1 2 3 4 -v 容器内路径 -v 卷名:容器内路径 -v /主机路径:容器内路径
设置容器对挂载目录的权限
1 2 3 4 5 6 7 8 ro readonly rw readwrite [root@bogon test]# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:ro nginx [root@bogon test]# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:rw nginx