Docker数据卷
数据卷挂载
命令方式挂载
命令挂载说明 -v [宿主机目录|挂载别名:]容器内目录[:ro|rw]
-
匿名挂载
docker run -it -v /root/volume1 --name centos1 centos /bin/bash
查看元数据信息
docker inspect centos1
"Mounts": [
{
"Type": "volume",
"Name": "9c795e1c6bc195860d2b93fdc9f1cc504f2ffaf7b8a46d0762d0584b123fe72d",
"Source": "/var/lib/docker/volumes/9c795e1c6bc195860d2b93fdc9f1cc504f2ffaf7b8a46d0762d0584b123fe72d/_data",
"Destination": "/root/volume1",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
-
具名挂载
docker run -it --name centos1 -v volume1:/root/volume1 centos /bin/bash
"Mounts": [
{
"Type": "volume",
"Name": "volume1",
"Source": "/var/lib/docker/volumes/volume1/_data",
"Destination": "/root/volume1",
"Driver": "local",
"Mode": "z",
"RW": true,
"Propagation": ""
}
],
docker run -it --name centos1 -v volume2:/root/volume2:ro centos /bin/bash
"Mounts": [
{
"Type": "volume",
"Name": "volume2",
"Source": "/var/lib/docker/volumes/volume2/_data",
"Destination": "/root/volume2",
"Driver": "local",
"Mode": "ro",
"RW": false,
"Propagation": ""
}
],
-
绑定挂载
通具名挂载类似,仅是名称更改为宿主机目录
docker run -it --name centos1 -v /data/volume1:/root/volume1 centos /bin/bash
"Mounts": [
{
"Type": "bind",
"Source": "/data/volume1",
"Destination": "/root/volume1",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
docker run -it --name centos1 -v /data/volume2:/root/volume2:ro centos /bin/bash
"Mounts": [
{
"Type": "bind",
"Source": "/data/volume2",
"Destination": "/root/volume2",
"Mode": "ro",
"RW": false,
"Propagation": "rprivate"
}
],
? 所有挂载的数据在宿主机和容器内都可以找到,并且容器删除后宿主机内的映射的数据卷信息不会被删除
Dockerfile挂载
-
创建Dockerfile文件 -
编写Dockerfile文件
FROM centos
VOLUME ["/root/volume1", "volume2"]
CMD echo "-- Dockerfile end --"
CMD /bin/bash
-
使用Dockerfile构建镜像
docker build -f Dockerfile -t mycentos .
[root@localhost ~]
Sending build context to Docker daemon 23.55kB
Step 1/4 : FROM centos
---> 5d0da3dc9764
Step 2/4 : VOLUME ["/root/volume1", "volume2"]
---> Running in 5a2fe4403328
Removing intermediate container 5a2fe4403328
---> 8b9b9c7683d8
Step 3/4 : CMD echo "-- Dockerfile end --"
---> Running in 5ee26484266a
Removing intermediate container 5ee26484266a
---> 811bfb554204
Step 4/4 : CMD /bin/bash
---> Running in 8c143cabb6ab
Removing intermediate container 8c143cabb6ab
---> fe7a7c3725ce
Successfully built fe7a7c3725ce
Successfully tagged mycentos:latest
-
启动容器
docker run -it --name mycentos1 mycentos /bin/bash
"Mounts": [
{
"Type": "volume",
"Name": "4d8b613894f543242d42013074ccca6079e4213caca015ec51adf281c611de07",
"Source": "/var/lib/docker/volumes/4d8b613894f543242d42013074ccca6079e4213caca015ec51adf281c611de07/_data",
"Destination": "/root/volume1",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "66d49c90c42bd312346559f1747d6fea317133155501da9c257cd9cf6b689083",
"Source": "/var/lib/docker/volumes/66d49c90c42bd312346559f1747d6fea317133155501da9c257cd9cf6b689083/_data",
"Destination": "volume2",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
数据卷容器共享
通过挂载共享数据卷,实现多个容器数据共享
docker run -it --name centos1 -v /data/volume1:/root/volume1 centos /bin/bash
docker run -it --name centos2 --volumes-from centos1 centos /bin/bash
运行结果
在此,实现多个容器数据卷共享。
文章有参考,如有侵权请联系作者
|