Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | Email

161.5. 卷管理

161.5.1. 列出卷

docker volume ls

# docker volume ls
DRIVER              VOLUME NAME
local               dbac41b6de88c75d2932d5949367b17f347f482977d508195375dbc71518ab27
			

161.5.2. 創建卷

# docker volume create --name WebVolume1
WebVolume1
			

# docker volume ls
DRIVER              VOLUME NAME
local               WebVolume1
local               dbac41b6de88c75d2932d5949367b17f347f482977d508195375dbc71518ab27			
			

161.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
			
			

161.5.4. 檢查卷

# docker volume inspect WebVolume1
[
    {
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/WebVolume1/_data",
        "Name": "WebVolume1",
        "Options": {},
        "Scope": "local"
    }
]
			

161.5.5. 刪除卷

# docker volume create AppVolume1
# docker volume rm AppVolume1
			

161.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
			

161.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
			
			

161.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