1)将Docker主机数据挂载到容器的三种方式:
volumes:Docker管理宿主机文件系统的一部分(/var/lib/docker/volumes)
bind mounts:可以存储在宿主机系统的任何位置
tmpfs:挂载存储在宿主机系统的内存中,而非写入宿主机的文件系统
[root@localhost ~]# ls /var/lib/docker/volumes/
metadata.db
2)docker volume
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
例如,docker volume create nginx-vol 创建一个数据卷 nginx-vol,挂载在/var/lib/docker/volumes/nginx-vol/_data;
docker run -itd --name=nginx-test --mount src=nginx-vol,dst=/usr/share/nginx/html nginx,使用数据卷nginx-vol 创建一个容器,对应容器中的路径是/usr/share/nginx/html;
会发现容器路径/usr/share/nginx/html 中的内容和宿主机路径/var/lib/docker/volumes/nginx-vol/_data中的内容一致
[root@localhost ~]# docker volume ls
DRIVER VOLUME NAME
[root@localhost ~]# docker volume create nginx-vol
nginx-vol
[root@localhost ~]# docker volume ls
DRIVER VOLUME NAME
local nginx-vol
[root@localhost ~]# docker volume inspect nginx-vol
[
{
"CreatedAt": "2019-08-11T19:21:12+08:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/nginx-vol/_data",
"Name": "nginx-vol",
"Options": {},
"Scope": "local"
}
]
[root@localhost ~]# docker run -itd --name=nginx-test --mount src=nginx-vol,dst=/usr/share/nginx/html nginx
3259c062853631cd60884daeeb970dfd283571eec65cf91ea932e0b2b8d2beff
[root@localhost ~]#
[root@localhost ~]# cd /var/lib/docker/volumes/nginx-vol/_data
[root@localhost _data]# ls
50x.html index.html
[root@localhost _data]# touch test01
[root@localhost _data]# docker exec nginx-test ls /usr/share/nginx/html
50x.html
index.html
test01
[root@localhost _data]#
将所有容器删除后,宿主机上的数据还在,重新创建一个容器(例如把80端口映射到本机8888端口)挂载相同路径;在宿主机创建一个html文件如test02.html,写入一些内容,浏览器输入<Host IP addr>:8888/test02.html
[root@localhost ~]# docker rm -f $(docker ps -q -a)
3259c0628536
b94f60530a2a
cc74fb080972
[root@localhost ~]# cd /var/lib/docker/volumes/nginx-vol/_data
[root@localhost _data]# ls
50x.html index.html test01
[root@localhost _data]# echo "<h1>It's rainy today.</h1>" > test02.html
[root@localhost _data]# cat test02.html
<h1>It's rainy today.</h1>
[root@localhost _data]# docker run -itd --name=nginx-test -p 8888:80 --mount src=nginx-vol,dst=/usr/share/nginx/html nginx
80e7a88804244734223965e90f14e902a04937cc1ad31ce00f5bdf77cf183d56
[root@localhost _data]#
网页显示效果如下:
由此,第一,可以实现数据持久化;第二,可以共享数据卷,扩展能力强
2.1)docker volume rm nginx-vol 清理数据卷
[root@localhost ~]# docker container stop nginx-test
nginx-test
[root@localhost ~]# docker container rm nginx-test
nginx-test
[root@localhost ~]# docker volume rm nginx-vol
nginx-vol
[root@localhost ~]#
2.2)起容器时不指定源路径,会自动生成一个匿名卷(相对地,之前的例子中创建的是命名卷)
[root@localhost ~]# docker run -itd --name=nginx-test -p 8888:80 --mount src=,dst=/usr/share/nginx/html nginx
ce86bc7461342daf29fcb758e899a95350dd7fed76818e1b1db891b0090050bf
[root@localhost ~]# cd /var/lib/docker/volumes/
[root@localhost volumes]# ls
6af6c2c94be43c940f2a16064f8de5102be5ec9ab94208579693d819b5212228 metadata.db
[root@localhost volumes]#
官方文档传送门 -> https://docs.docker.com/storage/volumes/#start-a-container-with-a-volume
3)bind mounts
需注意:第一,如果源目录不存在,不会自动创建,会抛出错误;
第二,如果挂载路径在容器中非空目录,则该目录现有的内容会被隐藏。
[root@localhost ~]# docker run -itd --name=nginx-test --mount type=bind,src=/app/test,dst=/usr/share/nginx/html nginx
docker: Error response from daemon: invalid mount config for type "bind": bind source path does not exist: /app/test.
See 'docker run --help'.
[root@localhost ~]# mkdir -p /app/test
[root@localhost ~]# docker run -itd --name=nginx-test --mount type=bind,src=/app/test,dst=/usr/share/nginx/html nginx
c02fd53772aabb26f4fe2784b80484bf8b5a5a35fb88bc21d42942564638d460
[root@localhost ~]# cd /app/test
[root@localhost test]# ls
[root@localhost test]# docker exec -it nginx-test bash
root@c02fd53772aa:/# cd /usr/share/nginx/html
root@c02fd53772aa:/usr/share/nginx/html# ls
root@c02fd53772aa:/usr/share/nginx/html# exit
exit
[root@localhost test]# touch testlocal
[root@localhost test]# docker exec -it nginx-test bash
root@c02fd53772aa:/# cd /usr/share/nginx/html
root@c02fd53772aa:/usr/share/nginx/html# ls
testlocal
root@c02fd53772aa:/usr/share/nginx/html#
可用 docker inspect <container ID | container name> 验证绑定(找到Mount)
官方文档传送门 -> https://docs.docker.com/storage/bind-mounts/#start-a-container-with-a-bind-mount
(End)
学习的视频路径 -> https://ke.qq.com/course/366769?taid=2769880244132017