通过dockerfile 挂载数据卷

一、通过dockerfile 挂载数据卷

1、创建dockerfile,内容如下:
[root@master home]# pwd
/home
[root@master home]# cat dockerfiletest 
FROM centos

VOLUME ["v01", "v02"]

CMD echo "----end-----"

CMD /bin/bash

[root@master home]# 

2、通过dockerfile 创建镜像
[root@master home]# docker build -f /home/dockerfiletest -t lkf_centos .
Sending build context to Docker daemon  7.168kB
Step 1/4 : FROM centos
 ---> 300e315adb2f
Step 2/4 : VOLUME ["v01", "v02"]
 ---> Running in 0be5372c2d95
Removing intermediate container 0be5372c2d95
 ---> db5ead0a07a1
Step 3/4 : CMD echo "----end-----"
 ---> Running in 74d2af77f420
Removing intermediate container 74d2af77f420
 ---> 16cd42f93da1
Step 4/4 : CMD /bin/bash
 ---> Running in 3db38fb02bf7
Removing intermediate container 3db38fb02bf7
 ---> 485f4e895ac9
Successfully built 485f4e895ac9
Successfully tagged lkf_centos:latest
[root@master home]# 
3、运行创建好的镜像
[root@master home]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
lkf_centos          latest              485f4e895ac9        22 seconds ago      209MB
centos              latest              300e315adb2f        8 months ago        209MB
[root@master home]# docker run -it lkf_centos /bin/bash
[root@73aae139e890 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  v01  v02

4、查看挂载信息
[root@master home]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS 
73aae139e890        lkf_centos          "/bin/bash"         9 minutes ago       Up 9 minutes 
[root@master home]# docker inspect 73aae139e890

二、制作数据卷容器

1、启动容器01
[root@master home]# docker run -it -d --name centos01 lkf_centos /bin/bash
2、启动容器02,数据卷继承01
[root@master home]# docker run -it -d --name centos02 --volumes-from centos01 lkf_centos /bin/bash
3、启动容器03,数据卷继承01
[root@master home]# docker run -it -d --name centos03 --volumes-from centos01 lkf_centos /bin/bash
4、测试,在容器01中新建一个文件,观察同步情况
# 新建文件text01.txt
[root@master home]# docker attach centos01
[root@3b026d389534 /]# cd v01
[root@3b026d389534 v01]# touch text01.txt
[root@3b026d389534 v01]# ls
text01.txt

# 在容器02中查看发现文件已同步
[root@master home]# docker attach centos02
[root@6e53d5f442a6 /]# ls /v01
text01.txt

# 在容器03中查看发现文件已同步
[root@master home]# docker attach centos03
[root@f918eda91154 /]# ls /v01
text01.txt

5、测试,在容器02中新建一个文件,观察同步情况
# 新建文件text02.txt
[root@master home]# docker attach centos02
[root@6e53d5f442a6 /]# touch /v01/test02.txt
[root@6e53d5f442a6 /]# ls /v01
test02.txt  text01.txt
[root@6e53d5f442a6 /]# 

# 在容器01中查看发现文件已同步
[root@master home]# docker attach centos01
[root@3b026d389534 v01]# ls
test02.txt  text01.txt
[root@3b026d389534 v01]# 

# 在容器03中查看发现文件已同步
[root@master home]# docker attach centos03
[root@f918eda91154 /]# ls /v01
test02.txt  text01.txt
[root@f918eda91154 /]# 

6、测试,删除容器01,在容器02中新建一个文件,观察同步情况
# 查看当前容器
[root@master ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS          NAMES
f918eda91154        lkf_centos          "/bin/bash"         17 hours ago        Up 2 minutes  centos03
6e53d5f442a6        lkf_centos          "/bin/bash"         17 hours ago        Up 2 minutes  centos02
3b026d389534        lkf_centos          "/bin/bash"         18 hours ago        Up 2 minutes  centos01
# 删除容器01
[root@master ~]# docker rm -f centos01
centos01
# 在容器02 中新建一个文件
[root@master ~]# docker exec -it centos02 /bin/bash
[root@6e53d5f442a6 /]# touch /v01/test-rm-c1.txt
[root@6e53d5f442a6 /]# ls /v01
test-rm-c1.txt  test02.txt  text01.txt
[root@6e53d5f442a6 /]# read escape sequence
# 查看容器03发现文件已同步
[root@master ~]# docker exec -it centos03 ls -l /v01
total 0
-rw-r--r-- 1 root root 0 Aug 24 01:08 test-rm-c1.txt
-rw-r--r-- 1 root root 0 Aug 23 08:08 test02.txt
-rw-r--r-- 1 root root 0 Aug 23 08:04 text01.txt
[root@master ~]# 

作者:likaifei

出处:https://www.cnblogs.com/likaifei/p/16707127.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   adai_kfl  阅读(394)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
more_horiz
keyboard_arrow_up light_mode palette
选择主题
点击右上角即可分享
微信分享提示