Volume特点
- 持久化存储文件
- 容器间共享文件
- 修改文件立即生效
docker提出的Vlolume概念,简单来说,就是目录
或者是文件,他可以绕过联合文件系统,而以
正常的文件或者目录的形式存于宿主机上
为容器添加卷
[root@bogon /]# docker run -dit --name test -v /test redis /bin/bash
root@9d7e5b04b3ac:/data# ls
root@9d7e5b04b3ac:/data# cd /
root@9d7e5b04b3ac:/# ls
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys test tmp usr var
root@9d7e5b04b3ac:/# cd test
root@e96a304b9270:/test# echo "hello,docker">txt
[root@bogon /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e96a304b9270 redis "docker-entrypoint..." 3 minutes ago Up 3 minutes 6379/tcp test
[root@bogon /]# docker inspect e96a304b9270
"Mounts": [
{
"Type": "volume",
"Name": "47e324fbd7beb5cc91e7b130108cab3e7c53cc86dcc1b6e3a020d18e878c8366",
"Source": "/var/lib/docker/volumes/47e324fbd7beb5cc91e7b130108cab3e7c53cc86dcc1b6e3a020d18e878c8366/_data",
"Destination": "/test",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "cd005b9b9b79bfade620ea744741f51e4c05956ed890a0215e07ab6d26b0c94a",
"Source": "/var/lib/docker/volumes/cd005b9b9b79bfade620ea744741f51e4c05956ed890a0215e07ab6d26b0c94a/_data",
"Destination": "/data",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
]
上面显示挂在了data和test到容器中
在宿主机中可以看到容器中的文件
[root@bogon /]# cat /var/lib/docker/volumes/47e324fbd7beb5cc91e7b130108cab3e7c53cc86dcc1b6e3a020d18e878c8366/_data/txt
hello,docker
删除容器和相应的卷
docker rm -f -v test
运行一个容器,使用前面容器使用的卷,卷可以被多个容器共享
docker run -it --name test2 --volumes-from test nginx /bin/bash
注意:上面创建一个test2容器,使用到了test的卷,这样,即使删除了容器test,相应的卷也不会被删除;在test2中修改文件,可以同步到test
本地文件映射到容器
docker run -dit -v /test1/:/test1/ redis /bin/bash
用途:数据库文件放到本地硬盘
备份
[root@bogon ~]#docker run -dit --name test01 -v /test1 /bin/bash
[root@bogon ~]#docker exec -it test01 /bin/bash
[root@f664684c31b1 /]# cat /test1/tt
xxxxxxxxx
[root@f664684c31b1 /]# exit
[root@bogon ~]#docker run --volumes-from test01 -v $(pwd):/backup redis tar cvf /backup/backup.tar /test1
[root@bogon ~]# ls
backup.tar
#创建一个redis镜像的容器,共享test01容器的卷,打包test1目录,备份到本地磁盘
这个命令运行一个redis镜像的一个容器实例,共享test01容器的卷,把/test1打包成backup.tar,然后复制到本地
恢复到新容器中
[root@bogon ~]# docker run -v $(pwd):/backup redis tar xf /backup/backup.tar
[root@bogon ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ca884f53b748 redis "docker-entrypoint..." 36 seconds ago Exited (0) 35 seconds ago jovial_heisenberg
1c60f322e204 redis "docker-entrypoint..." 25 minutes ago Exited (0) 23 minutes ago naughty_lalande
86abe2926755 redis "docker-entrypoint..." 25 minutes ago Exited (2) 25 minutes ago priceless_heyrovsky
c23fea7f72ba redis "docker-entrypoint..." 26 minutes ago Exited (2) 26 minutes ago thirsty_lovelace
f664684c31b1 b5e4ce650f0d "/bin/bash" About an hour ago Up About an hour test01
[root@bogon ~]# docker run -dit --name recovery --volumes-from ca884f53b748 redis /bin/bash
7087d6e9db40f4870655b86ef767271cfb073f4b2c7ebe98633c5c053bd111a1
#ca884f53b748是前面解压的容器
[root@bogon ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7087d6e9db40 redis "docker-entrypoint..." 7 seconds ago Up 6 seconds 6379/tcp recovery
f664684c31b1 b5e4ce650f0d "/bin/bash" About an hour ago Up About an hour test01
[root@bogon ~]# docker exec -it recovery /bin/bash
root@7087d6e9db40:/data# ls
test1
root@7087d6e9db40:/data# ls test1/
tt
root@7087d6e9db40:/data# cat test1/tt
xxxxxxxxx
root@7087d6e9db40:/data#
root@7087d6e9db40:/data# cd ..
root@7087d6e9db40:/# ls
backup bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@7087d6e9db40:/# ls backup/
anaconda-ks.cfg backup.tar centos_latest.tar
root@7087d6e9db40:/#