Docker学习笔记(二) volume介绍和操作
volume介绍和操作
一.volume 介绍
提供独立于容器之外的持久化存储,比如说运行一个数据库容器,数据库里面的数据就是需要被持久化的,volume就可以做到这个功能实现,并且还能共享容器与容器之间的数据。
二.volume 操作
方式一,以默认的目录挂载到指定的容器 目录中
docker run -d --name nginx -v /usr/share/nginx/html nginx
docker inspect nginx 查看所有的容器
}, "Mounts": [ { "Type": "volume", "Name": "daad412457c85a5ee10504928d9d2d642e45d5deb571ae762aa1e25e45925674", "Source": "/var/lib/docker/volumes/daad412457c85a5ee10504928d9d2d642e45d5deb571ae762aa1e25e45925674/_data", "Destination": "/usr/share/nginx/html", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], "Config": { "Hostname": "46e50083988e", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "80/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "NGINX_VERSION=1.15.2-1~stretch", "NJS_VERSION=1.15.2.0.2.2-1~stretch" ], "Cmd": [ "nginx", "-g", "daemon off;" ], "ArgsEscaped": true, "Image": "nginx", "Volumes": { "/usr/share/nginx/html": {} }, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>" }, "StopSignal": "SIGTERM" }, "NetworkSettings": { "Bridge": "", "SandboxID": "d23f93abdf77ecdd101c7c8ed63bf4b21c08aaee28120c53493fb30363ef2c06", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": null, "SandboxKey": "/var/run/docker/netns/d23f93abdf77", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "", "Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "MacAddress": "", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "e9a5c3a2efc45030b88a90fcf3db7d3e22282a98ba2862a00992db2b1a2cc82f", "EndpointID": "", "Gateway": "", "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "" } } } } ]
"Source": "/var/lib/docker/volumes/daad412457c85a5ee10504928d9d2d642e45d5deb571ae762aa1e25e45925674/_data",
"Destination": "/usr/share/nginx/html",
重点为此处,将本地的source 对应的目录挂载到 nginx 的访问页面目录 。
docker exec -it nginx /bin/bash 进入容器内部
方式二.将指定的当前目录的html目录挂载到指定的容器中
docker run -p 80:80 -d -v $PWD/html:/usr/share/nginx/html nginx 将当前目录下的html目录挂载到容器里面的/usr/share/nginx/html 目录
curl http://localhost: 输出的内容即为当前目录下的html目录中的index.html文件的内容
docker create -v $PWD/data:/var/mydata --name data_container ubuntu 、
将data_container 容器的/var/mydata 挂载到 ubuntu 容器里面
docker run -it --volumes-from data_container ubuntu /bin/bash
进入到Ubuntu容器里面 ,
mount 命令 查看所有挂载
发现 已经把 data_container 的/var/mydata挂载上来了
cd /var/mydata
touch whatever.txt
ctrl+d 或者 exit 退出当前容器
cd data 发现本地的目录中也创建 了一个 whatever.txt文件
多个容器之间可以互相挂载,以便做到数据共享。
- 点赞
- 收藏
- 关注作者
评论(0)