docker学习与实践
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker学习与实践相关的知识,希望对你有一定的参考价值。
五、数据管理1.数据卷
①创建一个数据卷
[[email protected] ~]# docker volume create v1
v1
[[email protected] ~]# docker volume ls
DRIVER VOLUME NAME
local v1
②查看数据卷信息
[[email protected] ~]# docker volume ls
DRIVER VOLUME NAME
local v1
[[email protected] ~]# docker volume inspect v1
[
{
"CreatedAt": "2018-06-04T01:47:39-04:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/v1/_data",
"Name": "v1",
"Options": {},
"Scope": "local"
}
]
③挂载容器
[[email protected] ~]# docker run -d -p 80:80 --name web --mount source=v1,target=/webapp nginx
3f315ab9ed576d5f0b72dc9e8c067331e0ef785a9577c2c3c6d2e74ec77e51fd
[[email protected] ~]# docker exec -it web /bin/bash
[email protected]:/# cd /webapp/
[email protected]:/webapp# touch ss.txt
[email protected]:/webapp# exit
exit
[[email protected] ~]# ls /var/lib/docker/volumes/v1/_data/
ss.txt
④查看容器信息
[[email protected] ~]# docker inspect -f "{{.Mounts}}" web
[{volume v1 /var/lib/docker/volumes/v1/_data /webapp local z true }]
⑤删除数据卷
[[email protected] ~]# docker volume rm v1
⑥挂载主机目录
[[email protected] ~]# docker run -d -p 80:80 --name web --mount type=bind,source=/dockerdata,target=/webapp nginx
67f90a8a2c6171bfbfce4c84606f0742adb7e283cdb45b488d47035b7f02871b
#挂载的主机目录默认权限是读写,也可以通过增加readonly来指定为只读
[[email protected] ~]# docker run -d -p 81:80 --name web2 --mount type=bind,source=/dockerdata,target=/webapp,readonly nginx
ced71fc7a97d251bfea388768e3e45cafe3a12680282d820e032d9845c74a1bf
#加上readonly后在容器内/webapp目录新建文件就会报错
[[email protected] ~]# docker exec -it web2 /bin/bash
[email protected]:/# cd webapp/
[email protected]:/webapp# touch ss
touch: cannot touch ‘ss‘: Read-only file system
[email protected]:/webapp# exit
exit
2.数据卷容器
如果用户需要在容器之间共享一些持续更新的数据,可以采用数据卷容器,数据卷容器其实是一个普通的容器,专门用来提供数据卷供其它容器挂载。
①创建一个数据卷并挂载数据卷到web1
[[email protected] ~]# docker volume create v1
v1
[[email protected] ~]# docker run -dit --mount source=v1,target=/tmp/test --name web1 centos
237879201e1c8fedae870af923083625ab8d0fb2b375f66784e1da4179e068c7
[[email protected] ~]# docker exec -it web1 ls -d /tmp/test
/tmp/test
②创建两个容器并从web1挂载数据卷
[[email protected] ~]# docker run -dit --volumes-from web1 --name db1 centos
0b656f36fe24ce835b94d2c891645962e6545e3e18c70bbc6a3e24edbd45f153
[[email protected] ~]# docker run -dit --volumes-from web1 --name db2 centos
4468244e0b8b7d6caa57801a2c98d1272c6fdb7d59e89ce704533b53fe969b70
[[email protected] ~]# docker inspect -f "{{".Mounts"}}" db2
[{volume v1 /var/lib/docker/volumes/v1/_data /tmp/test local true }]
③分别在web1和db1中新建两个测试文件
[[email protected] ~]# docker exec -it web1 touch /tmp/test/web1.txt
[[email protected] ~]# docker exec -it db1 touch /tmp/test/db1.txt
#在db2和本地主机中查看
[[email protected] ~]# docker exec -it db2 ls /tmp/test
db1.txt web1.txt
[[email protected] ~]# ls /var/lib/docker/volumes/v1/_data/
db1.txt web1.txt
④使用db2作为db3的容器数据卷
[[email protected] ~]# docker run -dit --volumes-from db2 --name db3 centos
7ccd5f862ce125121b2e216c3f312c1921a41df0214a327e957a15bd2041cf07
[[email protected] ~]# docker exec -it db3 ls /tmp/test
db1.txt web1.txt
⑤停止web1并查看关联容器的挂载文件
[[email protected] ~]# docker stop web1
web1
[[email protected] ~]# docker exec -it db3 ls /tmp/test
db1.txt web1.txt
3.使用数据卷容器迁移数据
①创建数据卷并挂载
[[email protected] ~]# docker volume create v2
v2
[[email protected] ~]# docker run -it --mount source=v2,target=/backup --name datamove centos
[[email protected] /]# cd /backup/
[[email protected] backup]# touch {a,b,c,d,ss}
[[email protected] backup]# ls
a b c d ss
②数据卷的备份
[[email protected] ~]# docker run --volumes-from datamove -v /dockerdata/:/back --name backup centos tar cvf /back/backup.tar /backup
tar: Removing leading `/‘ from member names
/backup/
/backup/a
/backup/b
/backup/c
/backup/d
/backup/ss
[[email protected] ~]# ls /dockerdata/
backup.tar
③创建一个容器savedata还原数据卷
[[email protected] ~]# docker run --volumes-from datamove -v /dockerdata/:/back --name savedata centos tar xvf /back/backup.tar
backup/
backup/a
backup/b
backup/c
backup/d
backup/ss
④创建一个容器挂载savedata
[[email protected] ~]# docker run -dit --volumes-from savedata --name savetest centos
faa008b4f18360b0bed3619f740ccc6a326d7e718020347bdb3027750d48ef60
[[email protected] ~]# docker exec -it savetest ls /backup
a b c d ss
以上是关于docker学习与实践的主要内容,如果未能解决你的问题,请参考以下文章