如果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