Docker数据卷容器(容器继承)

Docker数据卷容器(容器继承)

命名的容器挂载数据卷。其他容器通过挂载这个容器实现数据共享,挂载数据的容器 -> 称之为数据卷容器

创建DockerFile

shell 复制代码
FROM centos

VOLUME ["dataVolume01", "dataVolume02"]

CMD /bin/bash

构建镜像

shell 复制代码
docker build -f dockerFile02 -t mzw/centos .

启动容器

启动容器并命名为test01

shell 复制代码
docker run -it --name test01 mzw/centos

修改数据卷

进入数据卷

shell 复制代码
cd dataVolume01

创建文件并填写数据

shell 复制代码
echo hello >> a.txt

创建子容器

创建子容器,命名为test02,指定数据卷为容器test01

shell 复制代码
docker run -it --name test02 --volumes-from test01 mzw/centos

创建子容器,命名为test03,指定数据卷为容器test01

shell 复制代码
docker run -it --name test03 --volumes-from test01 mzw/centos

验证

  • 在容器test02中进入数据卷

    shell 复制代码
    cd dataVolume01
  • 查看内部数据

    shell 复制代码
    cat a.txt 


    test02与test01 数据卷内容一致

  • 修改test02内数据卷内容,查看test01内数据卷

    • 修改test02数据卷内容
    • 查看test01数据内容

      test01与test02 数据卷内容一致

此时删掉test01容器,02和03数据依然共享

数据卷的生命周期一直持续到没有容器使用为止

相关推荐
阿虎儿11 小时前
Docker安装(非sudo用户可用)
docker
甲鱼92913 小时前
MySQL 实战手记:日志管理与主从复制搭建全指南
运维
fetasty2 天前
rustfs加picgo图床搭建
docker
蝎子莱莱爱打怪2 天前
GitLab CI/CD + Docker Registry + K8s 部署完整实战指南
后端·docker·kubernetes
碳基沙盒3 天前
OpenClaw 多 Agent 配置实战指南
运维
小p3 天前
docker学习7:docker 容器的通信方式
docker
小p3 天前
docker学习5:提升Dockerfile水平的5个技巧
docker
小p3 天前
docker学习3:docker是怎么实现的?
docker
小p5 天前
docker学习: 2. 构建镜像Dockerfile
docker
小p5 天前
docker学习: 1. docker基本使用
docker