创建一个名为myvol的数据卷:
[root@localhost ~]# docker volume create myvol
myvol
[root@localhost ~]# docker volume ls
DRIVER VOLUME NAME
local myvol
查看数据卷:
[root@localhost ~]# docker volume inspect myvol
[
{
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/myvol/_data",
"Name": "myvol",
"Options": {},
"Scope": "local"
}
]
[root@localhost ~]#
启动一个Nginx容器并挂载数据卷:
[root@localhost ~]# docker run -d -p 8080:80 --name web -v myvol:/usr/share/nginx/html nginx:1.19.0
9a4f1ddca147077cb3799cf094027413981fd44be9ec0342b4180452b0dcb107
[root@localhost ~]# ls /var/lib/docker/volumes/myvol/_data
50x.html index.html
[root@localhost ~]#
[root@localhost ~]# curl http://localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost ~]#
修改默认页面,测试访问:
[root@localhost ~]# echo "test-volume" > //var/lib/docker/volumes/myvol/_data/index.html
[root@localhost ~]# curl http://localhost:8080
test-volume
[root@localhost ~]#
测试删除web容器,查看数据卷数据是否存在:
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a4f1ddca147 nginx:1.19.0 "/docker-entrypoin..." 3 minutes ago Up 3 minutes 0.0.0.0:8080->80/tcp web
[root@localhost ~]# docker rm -f web
web
[root@localhost ~]# docker volume ls
DRIVER VOLUME NAME
local myvol
[root@localhost ~]# cat /var/lib/docker/volumes/myvol/_data/index.html
test-volume
[root@localhost ~]#