Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

163.5. 卷管理

163.5.1. 列出卷

docker volume ls

			
# docker volume ls
DRIVER              VOLUME NAME
local               dbac41b6de88c75d2932d5949367b17f347f482977d508195375dbc71518ab27
			
		

163.5.2. 創建卷

			
# docker volume create --name WebVolume1
WebVolume1
			
		

			
# docker volume ls
DRIVER              VOLUME NAME
local               WebVolume1
local               dbac41b6de88c75d2932d5949367b17f347f482977d508195375dbc71518ab27			
			
		

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

163.5.4. 檢查卷

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

163.5.5. 刪除卷

			
# docker volume create AppVolume1
# docker volume rm AppVolume1
			
		

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

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

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

163.5.9. 只讀權限

/etc/redis/redis.conf:/etc/redis/redis.conf:ro 表示只讀權限

			
docker run \
-p 6379:6379 \
-v /var/lib/redis:/data \
-v /etc/redis/redis.conf:/etc/redis/redis.conf:ro \
--privileged=true \
--name redis \
-d docker.io/redis:latest redis-server /etc/redis/redis.conf