044、vloume声明周期管理(2019-03-07 周四)
如果Data Volume 中存放的是重要的应用数据,如何管理volume对应用至关重要。
前面学习了volume的创建、共享和使用,现在开始学习如何备份、恢复、迁移和销毁。
volume 备份
因为volume实际上是host文件系统中的目录和文件,所以volume的备份实际上是对文件系统的备份
下面例子中我们只需要定期备份host上的 /myregistry 目录即可
root@docker-lab:~/020# docker run -d -p 5000:5000 -v /myregistry:/var/lib/registry registry:2Unable to find image 'registry:2' locally # 运行本地Registry容器
2: Pulling from library/registry
cd784148e348: Pull complete
0ecb9b11388e: Pull complete
918b3ddb9613: Pull complete
5aa847785533: Pull complete
adee6f546269: Pull complete
Digest: sha256:1cd9409a311350c3072fe510b52046f104416376c126a479cef9a4dfe692cf57
Status: Downloaded newer image for registry:2
f9e7d22e21d6e0c03b146a3e2100c135c14203308c4323b0673c45eaea97030d
volume 恢复
volume的恢复也很简单,如果数据损坏了,直接把之前的备份恢复到 /myregistry 目录即可
volume 迁移
如果我们想使用最新版本的registry,这就涉及到数据迁移,也很简单
1、docker stop 当前registry
2、启动新版的的registry,并mount原有volume
volume 销毁
可以删除不在需要的volume,但一定要确保知道自己正在做什么,volume删除后数据是找不回来的。
docker 不会销毁 bind mount ,删除数据的工作只能由host负责。对于docker managed volume,在执行docker rm 删除容器时可以带上 -v 参数,docker 会将容器使用的volume一起删除,但前提是没有其他容器mount 该volume,目的是保护数据。
如果删除容器时没有带 -v 选项,这样就会产生孤儿 volume,我们可以使用docker volume 进行维护
root@docker-lab:~# docker volume --help
Usage: docker volume COMMAND
Manage volumes
Options:
Commands:
create Create a volume
inspect Display detailed information on one or more volumes
ls List volumes
prune Remove all unused local volumes
rm Remove one or more volumes
Run 'docker volume COMMAND --help' for more information on a command.
root@docker-lab:~# docker volume ls
DRIVER VOLUME NAME
local 0cbcb3731b6f8a2b9f957335d6e0792b00505cf2a5e2f86eea61cbcb2cb9e42d
local 1fd8e6ffd63362078a7ef3cf2e228c93eecbd72692cc5de3486b2b017759b88e
local 2abf327627f510cdb096768098aeeed5e71863c62ba4b82a258c8893fe70f3c9
local 3017874bfb6d78e034d699e82d3e8278b64e290ac70912d35c7256abbfa9e72f
local 353249c2f105095ef9ef3f2ee2a97f353f0e4f1354ee625657f5dd513df35088
local 3ffdd1201385f36eb3b05026fedcf71243211965979ba4a9f8be9b80fc9273de
local 643f80e53b0c53571f715ae7535f30d8b142b2321e7fd22fd29bf25fcf006059
local 69587f2f0d69dcbefbcdb44ec97f126b4e2f3a7326f4eab18da60639fbc15bd8
local 6d2b7b1116bc9b25326a1d00d0cafcabaf46577face06e7f52da409ce179ea68
local 812e94bd8072a74cb3b88f419f3a3d07ec9a7c68335dd2d1d54438de066dcacb
local 8275135d734d88e057d6998405357732ec64e0effd91bbe76f74a16ac856047d
local 8967124070bf1f6762dd24b0e16d2d82eedf03699a4cc47c577bf66a4abe3bee
local 9bda742b2b0149dc21a650a4a0eae5d635d28dbe7e6d861e4ea4b19f37347980
local cfeab0409cb87cc0306300b0b1ebb3cc6ec88d43cd077ff808102decbf6343da
local dd24468787a957f848f3d5175be447d7487188eccea27822a17bdf83bad8caef
local e942301ca877940d789f8be5ae4be211251d526e59ef789e273b161d573ec233
local f7a9fff2e85b66b33634c5f37bc8c65a6b9bdbf92613b2290157ba369e2700d2
local fb5ae539d81345d827aa241ff24d3cafb7aa9ac3b2c9139fcabfaf6513b983d3
root@docker-lab:~# docker volume rm fb5ae539d81345d827aa241ff24d3cafb7aa9ac3b2c9139fcabfaf6513b983d3
fb5ae539d81345d827aa241ff24d3cafb7aa9ac3b2c9139fcabfaf6513b983d3
root@docker-lab:~# docker volume ls
DRIVER VOLUME NAME
local 0cbcb3731b6f8a2b9f957335d6e0792b00505cf2a5e2f86eea61cbcb2cb9e42d
local 1fd8e6ffd63362078a7ef3cf2e228c93eecbd72692cc5de3486b2b017759b88e
local 2abf327627f510cdb096768098aeeed5e71863c62ba4b82a258c8893fe70f3c9
local 3017874bfb6d78e034d699e82d3e8278b64e290ac70912d35c7256abbfa9e72f
local 353249c2f105095ef9ef3f2ee2a97f353f0e4f1354ee625657f5dd513df35088
local 3ffdd1201385f36eb3b05026fedcf71243211965979ba4a9f8be9b80fc9273de
local 643f80e53b0c53571f715ae7535f30d8b142b2321e7fd22fd29bf25fcf006059
local 69587f2f0d69dcbefbcdb44ec97f126b4e2f3a7326f4eab18da60639fbc15bd8
local 6d2b7b1116bc9b25326a1d00d0cafcabaf46577face06e7f52da409ce179ea68
local 812e94bd8072a74cb3b88f419f3a3d07ec9a7c68335dd2d1d54438de066dcacb
local 8275135d734d88e057d6998405357732ec64e0effd91bbe76f74a16ac856047d
local 8967124070bf1f6762dd24b0e16d2d82eedf03699a4cc47c577bf66a4abe3bee
local 9bda742b2b0149dc21a650a4a0eae5d635d28dbe7e6d861e4ea4b19f37347980
local cfeab0409cb87cc0306300b0b1ebb3cc6ec88d43cd077ff808102decbf6343da
local dd24468787a957f848f3d5175be447d7487188eccea27822a17bdf83bad8caef
local e942301ca877940d789f8be5ae4be211251d526e59ef789e273b161d573ec233
local f7a9fff2e85b66b33634c5f37bc8c65a6b9bdbf92613b2290157ba369e2700d2
root@docker-lab:~# 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:
643f80e53b0c53571f715ae7535f30d8b142b2321e7fd22fd29bf25fcf006059
3017874bfb6d78e034d699e82d3e8278b64e290ac70912d35c7256abbfa9e72f
0cbcb3731b6f8a2b9f957335d6e0792b00505cf2a5e2f86eea61cbcb2cb9e42d
353249c2f105095ef9ef3f2ee2a97f353f0e4f1354ee625657f5dd513df35088
9bda742b2b0149dc21a650a4a0eae5d635d28dbe7e6d861e4ea4b19f37347980
1fd8e6ffd63362078a7ef3cf2e228c93eecbd72692cc5de3486b2b017759b88e
dd24468787a957f848f3d5175be447d7487188eccea27822a17bdf83bad8caef
812e94bd8072a74cb3b88f419f3a3d07ec9a7c68335dd2d1d54438de066dcacb
69587f2f0d69dcbefbcdb44ec97f126b4e2f3a7326f4eab18da60639fbc15bd8
cfeab0409cb87cc0306300b0b1ebb3cc6ec88d43cd077ff808102decbf6343da
8967124070bf1f6762dd24b0e16d2d82eedf03699a4cc47c577bf66a4abe3bee
6d2b7b1116bc9b25326a1d00d0cafcabaf46577face06e7f52da409ce179ea68
f7a9fff2e85b66b33634c5f37bc8c65a6b9bdbf92613b2290157ba369e2700d2
8275135d734d88e057d6998405357732ec64e0effd91bbe76f74a16ac856047d
2abf327627f510cdb096768098aeeed5e71863c62ba4b82a258c8893fe70f3c9
e942301ca877940d789f8be5ae4be211251d526e59ef789e273b161d573ec233
Total reclaimed space: 1.543GB
root@docker-lab:~# docker volume ls
DRIVER VOLUME NAME
local 3ffdd1201385f36eb3b05026fedcf71243211965979ba4a9f8be9b80fc9273de
root@docker-lab:~# docker inspect vc_data
"Mounts": [
{
"Type": "volume",
"Name": "3ffdd1201385f36eb3b05026fedcf71243211965979ba4a9f8be9b80fc9273de",
"Source": "/var/lib/docker/volumes/3ffdd1201385f36eb3b05026fedcf71243211965979ba4a9f8be9b80fc9273de/_data",
"Destination": "/usr/local/apache2/htdocs",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
root@docker-lab:~# docker rm -v vc_data
vc_data
root@docker-lab:~# docker volume ls
DRIVER VOLUME NAME
root@docker-lab:~#
单机 volume 总结:
1、docker为容器提供了两种存储资源:数据层 和 data volume
2、数据层包括镜像层和容器层,由storage driver管理
3、data volume有两种类型:bind mount 和docker managed volume
4、bind mount可以实现容器与host之间,容器与容器之间共享数据
5、volume container 是一种有更好移植性的容器间数据共享方案,特别是data-packed volume container
6、最后我们学习了如何备份、恢复、迁移和销毁data volume