《Netkiller Virtualization 手札》Docker 卷管理
阿新 • • 發佈:2022-05-02
本文節選擇《Netkiller Virtualization 手札》Docker 卷管理
1.5. 卷管理
1.5.1. 列出卷
docker volume ls
# docker volume ls
DRIVER VOLUME NAME
local dbac41b6de88c75d2932d5949367b17f347f482977d508195375dbc71518ab27
1.5.2. 建立卷
# docker volume create --name WebVolume1
WebVolume1
# docker volume ls DRIVER VOLUME NAME local WebVolume1 local dbac41b6de88c75d2932d5949367b17f347f482977d508195375dbc71518ab27
1.5.3. 掛在映象
# docker run -ti --rm -v WebVolume1:/www ubuntu
# docker run -ti --rm -v WebVolume1:/www docker.io/centos:7
檢視卷的掛載情況
# df | grep /www
/dev/vda1 20510332 7943940 11501484 41% /www
建立測試檔案
# mkdir -p /www/netkiller.cn/www.netkiller.cn # echo Helloworld > /www/netkiller.cn/www.netkiller.cn/index.html # cat /www/netkiller.cn/www.netkiller.cn/index.html Helloworld # exit exit
1.5.4. 檢查卷
# docker volume inspect WebVolume1
[
{
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/WebVolume1/_data",
"Name": "WebVolume1",
"Options": {},
"Scope": "local"
}
]
1.5.5. 刪除卷
# docker volume create AppVolume1 # docker volume rm AppVolume1
1.5.6. 銷燬所有未使用的卷
# docker volume prune
WARNING! This will remove all volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Volumes:
WebVolume1
3fd379f8c2cf8727d2e83e84e434ea1f122016957bd7cf78a0f05b6e5a69cf2b
app
Total reclaimed space: 11 B
1.5.7. 在多個容器間共享卷
容器一
# docker run -ti --name=Container1 -v DataVolume1:/opt/data ubuntu
容器二
# docker run -ti --name=Container2 --volumes-from Container1 ubuntu
進入容器一中檢視資料
# docker start -ai Container1
容器三,掛在只讀卷
# docker run -ti --name=Container3 --volumes-from Container2:ro ubuntu
刪除上面三個測試容易和卷
# docker rm Container1 Container2 Container3
# docker volume rm DataVolume1
1.5.8. 容器繫結本地檔案系統
Bind mount a volume (default [])
# docker run -it --name mycentos1 -v /www:/tmp/test docker.io/centos:7 /bin/bash
# docker run -d -v ~/logs:/var/log/nginx -p 80:80 -i nginx