数据卷的使用, 数据库可以保证如果容器出现问题但是数据不丢失的作用, 比如 MySQL/date 下的数据
或者 Nginx 根目录下的 index.html
查看数据卷
- [[email protected] ~]# docker volume ls
- DRIVER VOLUME NAME
创建数据卷
- [[email protected] ~]# docker volume create nginx-vol
- nginx-vol
查看已经创建了一个叫 nginx-vol 的数据卷
- [[email protected] ~]# docker volume ls
- DRIVER VOLUME NAME
- local nginx-vol
查看数据卷详细信息
[[email protected] ~]# docker volume inspect nginx-vol
运行一个叫 nginx-test 的容器并挂载数据卷, 源目录为 nginx-vol 目标目录为 Nginx 的 HTML 网页根目录下
- [[email protected] ~]# docker run -itd --name nginx-test --mount src=nginx-vol,dst=/usr/share/nginx/HTML nginx
- 8ea39f5eb3fac2d85039314117985abd5f6393548a0eb45e27946c424ebebfe8
进入网页目录下查看
- [[email protected] ~]# docker exec -it nginx-test bash
- [email protected]:/# cd /usr/share/nginx/HTML/
- [email protected]:/usr/share/nginx/HTML# ls
50x.HTML index.HTML
去挂载目录下查看是否也已经有数据卷了
- [[email protected] ~]# cd /var/lib/docker/volumes/
- [[email protected] volumes]# ls
- metadata.db nginx-vol
- [[email protected] volumes]# cd nginx-vol/
- [[email protected] nginx-vol]# ls
- _data
- [[email protected] nginx-vol]# cd _data/
- [[email protected] _data]# ls
50x.HTML index.HTML
删除所有容器查看数据库还存在
- [[email protected] ~]# docker rm -f $(docker container ls -qa)
- 8ea39f5eb3fa
- 5deb5f032783
- 4c6e1ee32733
- [[email protected] volumes]# cd nginx-vol/_data/
- [[email protected] _data]# ls
50x.HTML index.HTML
在跑一刚才的容器并指定端口映射
- [[email protected] _data]# docker run -itd --name nginx-test -p 89:80 --mount src=nginx-vol,dst=/usr/share/nginx/HTML nginx
- 6f6a742b02272b1eff38974e633e84033f257d6e1b8097c4b60202f1d3b6c451
在数据卷目录下并创建一个 a.HTML 的文件查看, 这里是起到数据卷挂载的持久化的作用
- [[email protected] _data]# curl 192.168.30.22:89
- 23456
- [[email protected] _data]# VIM a.HTML
- [[email protected] _data]# curl 192.168.30.22:89/a.HTML
- <h1>hello</h1>
我又切换了一个端口添加了映射, 这样的话可想而知如果启动 100 个这样的容器, 都是用这个数据卷, 扩展能力是非常强的, 只是端口不同
- [[email protected] ~]# docker run -itd --name nginx-test2 -p 90:80 --mount src=nginx-vol,dst=/usr/share/nginx/HTML nginx
- 8e3f213e652b31daef38f169240d2388386afeada29ff3367e712ccc98163f49
- [[email protected] _data]# curl 192.168.30.22:90
- 23456
- [[email protected] _data]# curl 192.168.30.22:90/a.HTML
- <h1>hello</h1>
来源: http://www.bubuko.com/infodetail-3102727.html