在前面已经学习了Docker的存储,https://www.cnblogs.com/zyxnhr/p/11830238.html,现在对前面的内容的一个补充,具体请参考https://www.cnblogs.com/zyxnhr/p/11830238.html
对docker的数据进行持久化
接上一章实验
[root@docker-server3 nginx]# docker run -d -v /data:/usr/share/nginx/html nginx:v1.4
693f7819a0301e6432d69cc1f278596d96aa8a980050b7d26815b44d9390bc66
[root@docker-server3 nginx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 693f7819a030 nginx:v1.4 "/build.sh nginx -g …" 4 seconds ago Up 3 seconds 80/tcp affectionate_chebyshev 3b26204c8758 nginx:v1.4 "/build.sh nginx -g …" 48 minutes ago Up 48 minutes 80/tcp thirsty_golick 0fef39ec7ffd nginx:v1.4 "/build.sh nginx -g …" 50 minutes ago Up 50 minutes 80/tcp flamboyant_dirac 9de02f005f66 nginx:v1.3 "/build.sh nginx -g …" 54 minutes ago Exited (0) 54 minutes ago flamboyant_poincare 593a35ccbbf9 nginx:v1.2 "nginx -g 'daemon of…" About an hour ago Up About an hour 80/tcp cool_clarke 55645329c490 nginx:v1.2 "nginx -g 'daemon of…" About an hour ago Up About an hour 80/tcp intelligent_carson 666927b25d2f nginx:v1.1 "nginx -g 'daemon of…" 2 hours ago Up 2 hours 80/tcp xenodochial_austin
[root@docker-server3 nginx]# docker inspect 693f7819a030 |grep IP
"LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "192.168.0.6", "IPPrefixLen": 24, "IPv6Gateway": "", "IPAMConfig": null, "IPAddress": "192.168.0.6", "IPPrefixLen": 24, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0,
[root@docker-server3 nginx]# curl http://192.168.0.6
自动创建/data目录
查看/data下数据
[root@docker-server3 nginx]# ll /data/ total 4 -rw-r--r-- 1 root root 12 Jan 2 17:54 index.html [root@docker-server3 nginx]# cat /data/index.html just a test
修改这个数据,会立即生效
[root@docker-server3 nginx]# echo aaaaa > /data/index.html [root@docker-server3 nginx]# curl http://192.168.0.6 aaaaa
进入docker容器内部查看
[root@docker-server3 nginx]# docker exec -it 693f7819a030 /bin/bash
[root@693f7819a030 html]# ll total 4 -rw-r--r-- 1 root root 6 Jan 2 22:57 index.html [root@693f7819a030 html]# pwd /usr/share/nginx/html [root@693f7819a030 html]# cat index.html aaaaa [root@693f7819a030 html]# exit exit
实现持久存储
使用Dockerfile的VOLUME指令
[root@docker-server3 nginx]# vi Dockerfile
FROM centos:7 WORKDIR /usr/share/nginx/html ADD build.sh /build.sh RUN yum install -y wget && wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo && yum -y install nginx && rm -rf index.html && echo "nginx in custom docker" > index.html && chmod +x /build.sh EXPOSE 80 ENTRYPOINT ["/build.sh"] VOLUME /usr/share/nginx/html CMD ["nginx","-g","daemon off;"]
[root@docker-server3 nginx]# docker build -t nginx:v1.5 .
[root@docker-server3 nginx]# docker run -d nginx:v1.5
a6c195265c4ba5cd786040988e3e5a11eb0c63b1d726657620a76438fcafdbfe
这里没有指定本机的挂载目录,查看挂载位置
[root@docker-server3 nginx]# docker inspect a6c195265c |grep -A 10 Mount
"MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "journald", "Config": {} }, -- "Mounts": [ { "Type": "volume", "Name": "fb7d3ec98bf52d43d832088b752417826a893d30719ea11186b3a72981db6b9c", "Source": "/var/lib/docker/volumes/fb7d3ec98bf52d43d832088b752417826a893d30719ea11186b3a72981db6b9c/_data", #挂载的本机位置 "Destination": "/usr/share/nginx/html", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }
默认挂载的本机位置是"Source": "/var/lib/docker/volumes/fb7d3ec98bf52d43d832088b752417826a893d30719ea11186b3a72981db6b9c/_data"
容器ID下的一个_data目录
[root@docker-server3 nginx]# cd /var/lib/docker/volumes/fb7d3ec98bf52d43d832088b752417826a893d30719ea11186b3a72981db6b9c/_data
[root@docker-server3 _data]# ll
这种挂载会将容器的数据映射出来
[root@docker-server3 _data]# docker run -d -v /tmp/test:/usr/share/nginx/html nginx:v1.5
2c081745d680548dabda33d1f50043e362dcafbc1ea7e7d878ecb2a70a8d525d [root@docker-server3 _data]# ll /tmp/test/ total 4 -rw-r--r-- 1 root root 12 Jan 2 18:12 index.html
这种方式就会覆盖掉内容
查看容器内时间
[root@docker-server3 _data]# docker exec -it 2c081745d6 /bin/bash
[root@2c081745d680 html]# date Thu Jan 2 23:14:54 UTC 2020 [root@2c081745d680 html]# exit exit
挂载文件
[root@docker-server3 _data]# docker run -d -v /etc/localtime:/etc/localtime nginx:v1.5
a4883fe9ce664110f7cd270a01d481aa18093f9375703c745fe93a89e6688f63 [root@docker-server3 _data]# docker exec -it a4883fe9c /bin/bash [root@a4883fe9ce66 html]# date Thu Jan 2 18:18:08 EST 2020
挂载多个目录
[root@docker-server3 _data]# docker run -d -v /etc/localtime:/etc/localtime -v /aaaa:/aaaa -v /tmp/test2:/usr/share/nginx/html nginx:v1.5
cfafe4c3c1b5d940679aa7eff49096bb4cbc2f112a2007ecf17320ad5e4209ea [root@docker-server3 _data]# docker exec -it cfafe /bin/bash #进入查看 [root@cfafe4c3c1b5 html]# date Thu Jan 2 18:21:13 EST 2020 [root@cfafe4c3c1b5 html]# ll /usr/share/nginx/html/ total 4 -rw-r--r-- 1 root root 12 Jan 2 18:21 index.html [root@cfafe4c3c1b5 html]# df -hT Filesystem Type Size Used Avail Use% Mounted on overlay overlay 47G 4.1G 43G 9% / tmpfs tmpfs 64M 0 64M 0% /dev tmpfs tmpfs 910M 0 910M 0% /sys/fs/cgroup shm tmpfs 64M 0 64M 0% /dev/shm /dev/mapper/centos-root xfs 47G 4.1G 43G 9% /aaaa #挂载的 tmpfs tmpfs 910M 0 910M 0% /proc/asound tmpfs tmpfs 910M 0 910M 0% /proc/acpi tmpfs tmpfs 910M 0 910M 0% /proc/scsi tmpfs tmpfs 910M 0 910M 0% /sys/firmware
设置挂载目录权限
[root@docker-server3 _data]# docker run -d -v /etc/localtime:/etc/localtime:ro -v /aaaa:/aaaa:rw -v /tmp/test2:/usr/share/nginx/html:ro nginx:v1.5
5ee7277878743c4f9053eb2f7af1dbf19f7bea30dca9992cd7f1b2ffea7bd71c [root@docker-server3 _data]# docker exec -it 5ee727787 /bin/bash [root@5ee727787874 html]# touch test.html touch: cannot touch 'test.html': Read-only file system [root@5ee727787874 html]# echo 111 > /etc/localtime bash: /etc/localtime: Read-only file system [root@5ee727787874 html]# touch /aaaa/test.html [root@5ee727787874 html]# echo 1111 > /aaaa/test.html [root@5ee727787874 html]# cat /aaaa/test.html 1111
但是可以通过宿主机修改
[root@docker-server3 _data]# echo 2222 /tmp/test2/index.html
2222 /tmp/test2/index.html [root@docker-server3 _data]# echo 2222 > /tmp/test2/index.html [root@docker-server3 _data]# cat /tmp/test2/index.html 2222 [root@docker-server3 _data]# docker exec -it 5ee727787 /bin/bash [root@5ee727787874 html]# cat index.html 2222
删除容器顺便删除挂载的卷,可以指定-v参数
例如:
docker rm -fv 5ee727787874
这种方式可以删除Dockerfile内默认挂载的容器的卷目录,但是对于在外面指定挂载的目录,是没有办法删除的,因为这个目录有可能其他容器在使用,同时还有可能没有权限删除外部的目录
测试
[root@docker-server3 ~]# docker run -d -v /data:/usr/share/nginx/html nginx:v1.5
8c5cd3b90c85c22a7e1d6847bd17fae9cef973d6d2e7a7327b6f35a18ebce8de
[root@docker-server3 ~]# docker rm -fv 8c5cd3b90c85
[root@docker-server3 ~]# ll /data/
自定义目录没有被删除
使用Dockerfile默认的挂载
[root@docker-server3 ~]# docker run -d nginx:v1.5
63710e4226db67455efe7831e27c4151d8e509624d32f97482f634aac1474c5e
[root@docker-server3 ~]# docker inspect 63710e4226db|grep Mounts -A 10
"Mounts": [ { "Type": "volume", "Name": "24dc1433bb9da5cf0739367e03cbdde2ed8a9db8ab320dee72cd3d8629a3d0cc", "Source": "/var/lib/docker/volumes/24dc1433bb9da5cf0739367e03cbdde2ed8a9db8ab320dee72cd3d8629a3d0cc/_data", #默认挂载位置 "Destination": "/usr/share/nginx/html", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }
[root@docker-server3 ~]# ll /var/lib/docker/volumes/24dc1433bb9da5cf0739367e03cbdde2ed8a9db8ab320dee72cd3d8629a3d0cc/_data
删除容器,但是不指定-v参数
[root@docker-server3 ~]# docker rm -f 63710e4226db
查看容器已经删除,目录还在
[root@docker-server3 ~]# docker ps -a
[root@docker-server3 ~]# ll /var/lib/docker/volumes/24dc1433bb9da5cf0739367e03cbdde2ed8a9db8ab320dee72cd3d8629a3d0cc/_data
重新创建容器,并使用-v参数删除
[root@docker-server3 ~]# docker run -d nginx:v1.5
5347b07f6d15245c17bbe92603a26a610be79788a6fddc1bf9d61eb7a138593b
[root@docker-server3 ~]# docker inspect 5347b07f6d15|grep Mounts -A 10
"Mounts": [ { "Type": "volume", "Name": "cc5c93a9923f719873695eecf7309c32d199831da4259ba7451100ccd6efe911", "Source": "/var/lib/docker/volumes/cc5c93a9923f719873695eecf7309c32d199831da4259ba7451100ccd6efe911/_data", "Destination": "/usr/share/nginx/html", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }
[root@docker-server3 ~]# ll /var/lib/docker/volumes/cc5c93a9923f719873695eecf7309c32d199831da4259ba7451100ccd6efe911/_data
[root@docker-server3 ~]# docker rm -fv 5347b07f6d15
[root@docker-server3 ~]# ll /var/lib/docker/volumes/cc5c93a9923f719873695eecf7309c32d199831da4259ba7451100ccd6efe911/_data
ls: cannot access /var/lib/docker/volumes/cc5c93a9923f719873695eecf7309c32d199831da4259ba7451100ccd6efe911/_data: No such file or directory
使用-v参数,默认挂载的卷已经被删除
博主声明:本文的内容来源主要来自誉天教育晏威老师,由本人实验完成操作验证,需要的博友请联系誉天教育(http://www.yutianedu.com/),获得官方同意或者晏老师(https://www.cnblogs.com/breezey/)本人同意即可转载,谢谢!