欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Docker数据卷

时间:2023-05-16
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.55kBStep 1/4 : FROM centos ---> 5d0da3dc9764Step 2/4 : VOLUME ["/root/volume1", "volume2"] ---> Running in 5a2fe4403328Removing intermediate container 5a2fe4403328 ---> 8b9b9c7683d8Step 3/4 : CMD echo "-- Dockerfile end --" ---> Running in 5ee26484266aRemoving intermediate container 5ee26484266a ---> 811bfb554204Step 4/4 : CMD /bin/bash ---> Running in 8c143cabb6abRemoving intermediate container 8c143cabb6ab ---> fe7a7c3725ceSuccessfully built fe7a7c3725ceSuccessfully 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 从其他容器挂载

运行结果

在此,实现多个容器数据卷共享。

文章有参考,如有侵权请联系作者

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。