正常情况下,删除容器,容器中所有的文件也会被删除。
数据卷的作用:
1:持久化容器运行过程中产生的数据文件
2:实现多个容器间的文件共享。
在容器中管理数据主要有两种方式
• 数据卷(Volumes)
•
二、docker 数据卷的操作
数据卷 是一个可供一个或多个容器使用的特殊目录,它绕过 UFS,可以提供很多有用的特性:
1、数据卷可以在容器之间共享和重用
2、对数据卷的修改会立马生效
3、对数据卷的更新,不会影响镜像
4、数据卷默认会一直存在,即使容器被删除
注意:数据卷的使用,类似于 Linux 下对目录或文件进行 mount,镜像中的被指定为挂载点的目录中的文件会隐藏掉,能显示看的是挂载的数据卷。
docker数据卷的目录操作
1、创建数据卷
docker volume creste 数据卷名称 [root@inode3 ~]# docker volume create ywx ywx
2、查看所有的数据卷
docker volume ls [root@inode3 ~]# docker volume ls DRIVER VOLUME NAME local ywx
3、查看容器使用数据卷的详细信息
docker volume inspect volume-name [root@inode3 ~]# docker volume inspect ywx [ { "CreatedAt": "2020-03-07T17:43:06+08:00", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/ywx/_data", "Name": "ywx", "Options": {}, "Scope": "local" } ] #"Mountpoint": "/var/lib/docker/volumes/ywx/_data" 数据卷的所在路径
docker run --name web4 -d -p 84:80 --mount source=ywx,target=/usr/share/nginx/html nginx:latest 或者 docker run --name web4 -d -p 84:80 -v ywx:/usr/share/nginx/html nginx:latest #ywx为创建的数据卷
启动一个nginx的web1容器,将数据卷ywx挂在到容器的/usr/share/nginx/html目录中
方法一: -v选项
docker run -d -p 80:80 -v ywx:/usr/share/nginx/html --name web1 nginx:latest [root@inode3 ~]# docker run -d -p 80:80 -v ywx:/usr/share/nginx/html --name web1 nginx:latest 10d439e628573c6278ad3a7be2b58b472bb3b8a2b78bf5f6b4de7f479d0cbf4a [root@inode3 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 10d439e62857 nginx:latest "nginx -g 'daemon of…" 4 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp web1 查看挂在的数据卷信息 docker inspect web1 #查看"Mounts"信息 "Mounts": [ { "Type": "volume", "Name": "ywx", "Source": "/var/lib/docker/volumes/ywx/_data",#数据卷的路径信息 "Destination": "/usr/share/nginx/html",#挂载到容器中的路径信息 "Driver": "local", "Mode": "z", "RW": true, "Propagation": "" } ],
[root@inode3 ~]# curl 192.168.32.103 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { 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>
echo "docker web1 index.html" > /var/lib/docker/volumes/ywx/_data/index.html
再次访问web1容器
[root@inode3 ~]# curl 192.168.32.103 docker web1 index.html
方式二: --mount
另外启动一个nginx的web2容器,将数据卷ywx挂在到容器的/usr/share/nginx/html目录中
docker run -d -p 81:80 --name web2 --mount source=ywx,target=/usr/share/nginx/html nginx:latest [root@inode3 ~]# docker run -d -p 81:80 --name web2 --mount source=ywx,target=/usr/share/nginx/html nginx:latest a0b80e4555e61258b5758df7c9c4d3a4fe80e6fc0fc041a539c2b006c8771482
[root@inode3 ~]# docker inspect web2 "Mounts": [ { "Type": "volume", "Name": "ywx", "Source": "/var/lib/docker/volumes/ywx/_data", "Destination": "/usr/local/nginx/html", "Driver": "local", "Mode": "z", "RW": true, "Propagation": "" } ],
访问容器web2
[root@inode3 ~]# curl 192.168.32.103:81 docker web1 index.html
访问的页面信息与web1相同
[root@inode3 ~]# docker volume ls DRIVER VOLUME NAME local ywx 启用一个容器web3挂在一个不存在的数据卷king [root@inode3 ~]# docker run --name web3 -d -p 83:80 -v king:/usr/share/nginx/html nginx:latest c92a82c5f0ad5c4f312f751f2355ecde3cc955dd11d98e7615b059c3ee2d397f [root@inode3 ~]# docker volume ls DRIVER VOLUME NAME local king local ywx -v选项会自动创建数据卷king
[root@inode3 ~]# docker volume ls DRIVER VOLUME NAME local king local ywx 启用一个容器web4挂在一个不存在的数据卷seal [root@inode3 ~]# docker run --name web4 -d -p 84:80 --mount source=seal,target=/usr/share/nginx/html nginx:latest 6262d51e0a7d08a62aac2b6b5db14c160f53f36c137cd211663f6002f4e6cd4e [root@inode3 ~]# docker volume ls DRIVER VOLUME NAME local king local seal local ywx
dockers volume rm volume-name [root@inode3 ~]# docker volume ls DRIVER VOLUME NAME local king local seal local ywx
数据卷 是被设计用来持久化数据的,它的生命周期独立于容器,Docker 不会在容器被删除后自动删除 数据卷,并且也不存在垃圾回收这样的机制来处理没有任何容器引用的数据卷。
无主的数据卷可能会占据很多空间,要清理请使用以下命令
docker volume prune
[root@inode3 ~]# docker volume rm seal Error response from daemon: remove seal: volume is in use - [6262d51e0a7d08a62aac2b6b5db14c160f53f36c137cd211663f6002f4e6cd4e] 如果有容器在使用,删除时会报错 首先停止使用数据卷的容器 [root@inode3 ~]# docker stop 6262d51e0a7d08a62aac2b6b5db14c160f53f36c137cd211663f60 6262d51e0a7d08a62aac2b6b5db14c160f53f36c137cd211663f60 再删除该容器 [root@inode3 ~]# docker rm -f 6262d51e0a7d08a62aac2b6b5db14c160f53f36c137cd211663f60 最后删除数据卷 [root@inode3 ~]# docker volume rm seal seal [root@inode3 ~]# docker volume ls DRIVER VOLUME NAME local king local ywx
删除没有使用的数据卷king和seal [root@inode3 ~]# docker volume prune WARNING! This will remove all local volumes not used by at least one container. Are you sure you want to continue? [y/N] y Deleted Volumes: king seal Total reclaimed space: 1.106kB [root@inode3 ~]# docker volume ls DRIVER VOLUME NAME local ywx
挂在主机目录就是把宿主机的一个命令挂在到容器中去
使用-v参数来挂在宿主机下的/web目录,把它挂在载nginx web1的容器中去
[root@inode3 ~]# docker run -d -p 81:80 --name web1 -v /web:/usr/share/nginx/html nginx:latest c5ba05a6efc549daaae50e4d039869d279dce0c4c4c19b2104906e69a8e1bb2b 查看挂载信息 [root@inode3 ~]# docker inspect web1 "Mounts": [ { "Type": "bind", "Source": "/web",#宿主机的目录 "Destination": "/usr/share/nginx/html",#挂在到容器中的目录 "Mode": "", "RW": true, "Propagation": "rprivate" } ], 访问web1容器 [root@inode3 ~]# curl 192.168.32.103:81 <html> <head><title>403 Forbidden</title></head> <body> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.17.8</center> </body> </html> 没有页面信息,在宿主机的/web目录中创建index.html页面,再次访问web1容器 echo "mount /web to web1-/usr/share/nginx/html" > /web/index.html [root@inode3 ~]# echo "mount /web to web1-/usr/share/nginx/html" > /web/index.html [root@inode3 ~]# curl 192.168.32.103:81 mount /web to web1-/usr/share/nginx/html 访问成功
[root@inode3 ~]# docker run -d -p 82:80 --name web2 --mount type=bind,source=/web,target=/usr/share/nginx/html nginx:latest 73143f0adc51c43c2b6584c675e17ec0778f1e27bc6d19fd1043e8a3b18c95f0
[root@inode3 ~]# docker inspect web2 "Mounts": [ { "Type": "bind", "Source": "/web", "Destination": "/usr/share/nginx/html", "Mode": "", "RW": true, "Propagation": "rprivate" } ], 访问容器web2 [root@inode3 ~]# curl 192.168.32.103:82 mount /web to web1-/usr/share/nginx/html
--mount在挂载不存在的目录时,会报错的 [root@inode3 ~]# ll /ywx ls: cannot access /ywx: No such file or directory [root@inode3 ~]# docker run -d -p 83:80 --name web3 --mount type=bind,source=/ywx,target=/usr/share/nginx/html nginx:latest docker: Error response from daemon: invalid mount config for type "bind": bind source path does not exist: /ywx. See 'docker run --help'. -v在挂载不存在的目录时,会先创建目录,再来挂载 [root@inode3 ~]# ll /ywx ls: cannot access /ywx: No such file or directory [root@inode3 ~]# docker run -d -p 83:80 --name web3 -v /ywx:/usr/share/nginx/html nginx:latest 9dbb1670c1d774a9161f7cc29b0b2873ae531635acec9989473ef02d191ef20e [root@inode3 ~]# ll -d /ywx drwxr-xr-x 2 root root 6 Mar 7 19:00 /ywx
挂载只读目录只能使用--mount参数
把宿主机的/tmp目录以只读的方式挂载到容器web1中的/usr/share/nginx/html,则在容器web1将不能对/usr/share/nginx/html进行修改 [root@inode3 ~]# docker run -d -p 81:80 --name web1 --mouth type=bind,source=/ywx,target=/usr/share/nginx/html,readonly nginx:latest 进入容器测试 [root@inode3 ~]# docker run -d -p 81:80 --name web1 --mount type=bind,source=/ywx,target=/usr/share/nginx/html,readonly nginx:latest 46198dc62481956d2bc918b8139b440ba0553be1668c14770f9c80f605b514ca [root@inode3 ~]# docker exec -it web1 /bin/bash root@46198dc62481:/# cd /usr/share/nginx/html/ root@46198dc62481:/usr/share/nginx/html# touch aa touch: cannot touch 'aa': Read-only file system