docker数据卷的管理和使用

数据卷的使用,数据库可以保证如果容器出现问题但是数据不丢失的作用,比如MySQL/date下的数据

或者Nginx根目录下的index.html

查看数据卷

[root@docker ~]# docker volume ls

DRIVER              VOLUME NAME

创建数据卷

[root@docker ~]# docker volume create nginx-vol

nginx-vol

查看已经创建了一个叫nginx-vol的数据卷

[root@docker ~]# docker volume ls

DRIVER              VOLUME NAME

local               nginx-vol

查看数据卷详细信息

[root@docker ~]# docker volume inspect nginx-vol

运行一个叫nginx-test的容器并挂载数据卷,源目录为nginx-vol 目标目录为Nginxhtml网页根目录下

[root@docker ~]# docker run -itd --name nginx-test --mount src=nginx-vol,dst=/usr/share/nginx/html nginx

8ea39f5eb3fac2d85039314117985abd5f6393548a0eb45e27946c424ebebfe8

进入网页目录下查看

[root@docker ~]# docker exec -it nginx-test bash

root@8ea39f5eb3fa:/# cd /usr/share/nginx/html/

root@8ea39f5eb3fa:/usr/share/nginx/html# ls

50x.html  index.html

去挂载目录下查看是否也已经有数据卷了

[root@docker ~]# cd /var/lib/docker/volumes/

[root@docker volumes]# ls

metadata.db  nginx-vol

[root@docker volumes]# cd nginx-vol/

[root@docker nginx-vol]# ls

_data

[root@docker nginx-vol]# cd _data/

[root@docker _data]# ls

50x.html  index.html

删除所有容器查看数据库还存在

[root@docker ~]# docker rm -f $(docker container ls -qa)

8ea39f5eb3fa

5deb5f032783

4c6e1ee32733

 

[root@docker volumes]# cd nginx-vol/_data/

[root@docker _data]# ls

50x.html  index.html

 

在跑一刚才的容器并指定端口映射

[root@docker _data]# docker run -itd --name nginx-test -p 89:80 --mount src=nginx-vol,dst=/usr/share/nginx/html nginx

6f6a742b02272b1eff38974e633e84033f257d6e1b8097c4b60202f1d3b6c451

在数据卷目录下并创建一个a.html的文件查看,这里是起到数据卷挂载的持久化的作用

[root@docker _data]# curl 192.168.30.22:89

23456

[root@docker _data]# vim a.html

[root@docker _data]# curl 192.168.30.22:89/a.html

<h1>hello</h1>

 

我又切换了一个端口添加了映射,这样的话可想而知如果启动100个这样的容器,都是用这个数据卷,扩展能力是非常强的,只是端口不同

[root@docker ~]# docker run -itd --name nginx-test2 -p 90:80 --mount src=nginx-vol,dst=/usr/share/nginx/html nginx

8e3f213e652b31daef38f169240d2388386afeada29ff3367e712ccc98163f49

[root@docker _data]# curl 192.168.30.22:90

23456

[root@docker _data]# curl 192.168.30.22:90/a.html

<h1>hello</h1>

原文地址:https://www.cnblogs.com/zc1741845455/p/11078845.html