Docker数据卷
数据卷挂载
命令方式挂载
命令挂载说明 -v [宿主机目录|挂载别名:]容器内目录[:ro|rw]
-
匿名挂载
# 匿名挂载 docker run -it -v /root/volume1 --name centos1 centos /bin/bash # -d 后台运行 # -v 数据卷挂载 容器内目录 # --name 命名容器
查看元数据信息
# 查看元数据 docker inspect centos1 # 挂载信息如下 "Mounts": [ { "Type": "volume", "Name": "9c795e1c6bc195860d2b93fdc9f1cc504f2ffaf7b8a46d0762d0584b123fe72d", "Source": "/var/lib/docker/volumes/9c795e1c6bc195860d2b93fdc9f1cc504f2ffaf7b8a46d0762d0584b123fe72d/_data", "Destination": "/root/volume1", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], # Type 挂载类型 # Name 匿名挂载的随机名称 # Source 宿主机挂载目的地址 # Destination 容器内挂载的目录 # RW 是否可读可写
-
具名挂载
# 具名挂载 docker run -it --name centos1 -v volume1:/root/volume1 centos /bin/bash # -v 数据卷挂载 映射名称:容器内目录 # 挂载信息如下 "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 # -v 数据卷挂载 映射名称:容器内目录:权限 # ro 权限:只读权限,容器内挂载目录无法创建文件或目录 # 挂载信息如下 "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 # -v 数据卷挂载 宿主机目录:容器内目录 # 挂载信息如下 "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 # -v 数据卷挂载 宿主机目录:容器内目录:权限 # ro 权限:只读权限,容器内挂载目录无法创建文件或目录 # 挂载信息如下 "Mounts": [ { "Type": "bind", "Source": "/data/volume2", "Destination": "/root/volume2", "Mode": "ro", "RW": false, "Propagation": "rprivate" } ],
所有挂载的数据在宿主机和容器内都可以找到,并且容器删除后宿主机内的映射的数据卷信息不会被删除
Dockerfile挂载
-
创建Dockerfile文件
-
编写Dockerfile文件
# 基于 centos镜像 FROM centos # 挂载数据卷列表(匿名挂载) VOLUME ["/root/volume1", "volume2"] # 输出命令 CMD echo "-- Dockerfile end --" # 命令 CMD /bin/bash
-
使用Dockerfile构建镜像
# 后面的 . 一定要有 docker build -f Dockerfile -t mycentos . # -f 指定Dockerfile文件 # -t 指定生成的镜像名称 名称[:版本tag] # 构建成功示例 [root@localhost ~]# docker build -f Dockerfile -t mycentos . 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
# --volumes-from 从其他容器挂载
运行结果
在此,实现多个容器数据卷共享。
文章有参考,如有侵权请联系作者