Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

104.7. 卷管理

104.7.1. 列出卷

docker volume ls

			
# docker volume ls
DRIVER              VOLUME NAME
local               dbac41b6de88c75d2932d5949367b17f347f482977d508195375dbc71518ab27
			
		

104.7.2. 创建卷

			
# docker volume create --name WebVolume1
WebVolume1
			
		

			
# docker volume ls
DRIVER              VOLUME NAME
local               WebVolume1
local               dbac41b6de88c75d2932d5949367b17f347f482977d508195375dbc71518ab27			
			
		

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

104.7.4. 检查卷

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

104.7.5. 删除卷

			
# docker volume create AppVolume1
# docker volume rm AppVolume1
			
		

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

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

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

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