Docker数据卷实现持久化存储

宿主机和容器手工交互数据

查看

查看所有镜像并创建运行一台nginx容器

[root@localhost ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              daee903b4e43        8 days ago          133MB
hello-world         latest              bf756fb1ae65        10 months ago       13.3kB
centos              6.9                 2199b8eb8390        20 months ago       195MB
centos              7.5.1804            cf49811e3cdb        20 months ago       200MB
[root@localhost ~]# docker container run -d --name nginx_server -p 80:80 daee903b4e43
2d97843b6430beafaee71d59dfa67443e0a2179cf2bbce5233cd300e696561bf

浏览器测试成功
在这里插入图片描述

连接

连接nginx容器,nginx的网站根目录为 /usr/share/nginx/html/

[root@localhost ~]# docker container exec -it nginx_server /bin/bash
root@2d97843b6430:/# 

在宿主机拷贝首页文件到nginx根目录下

[root@localhost ~]# cat /opt/html/index.html 
hello myfriend
[root@localhost ~]# docker container cp /opt/html/index.html nginx_server:/usr/share/nginx/html/
[root@localhost ~]# 

测试

浏览器测试修改成功
在这里插入图片描述
拷贝nginx容器下的文件到本地

[root@localhost ~]# docker container cp nginx_server:/usr/share/nginx/html/50x.html  /opt/html/
[root@localhost ~]# ls /opt/html/
50x.html  index.html
[root@localhost ~]# 

Volume实现宿主机和容器的数据共享

创建容器

创建时添加 -v 参数即可
创建新的nginx容器
映射端口为8080,文件映射位置为/opt/html/

[root@localhost ~]# docker container run -d --name nginx_server2 -p 8080:80 -v /opt/html/:/usr/share/nginx/html/  nginx:latest
446a49e3709e8435d0dbb16d157af418456db5fa2e92f9e9dad9062716708718

修改文件

修改宿主机本地的首页文件

root@localhost ~]# cd /opt/html/
[root@localhost html]# cat index.html 
hello myfriend
[root@localhost html]# echo "docker is running" > index.html 

连接nginx容器查看首页文件已经被修改

[root@localhost ~]# docker container exec -it nginx_server2 /bin/bash
root@446a49e3709e:/# cd /usr/share/nginx/html/
root@446a49e3709e:/usr/share/nginx/html# cat index.html 
docker is running
root@446a49e3709e:/usr/share/nginx/html# 

浏览器测试

在这里插入图片描述

数据卷容器

作用: 在集中管理集群中,大批量的容器都需要挂载相同的多个数据卷时,可以采用数卷容器进行统一管理

在宿主机模拟数据目录

mkdir -p /opt/Volume/a
mkdir -p /opt/Volume/b
touch /opt/Volume/a/a.txt
touch /opt/Volume/b/b.txt

启动数据卷容器

启动之后按 ctrl +p+ q 退出,让容器后台运行

[root@localhost ~]# docker container run -it --name nginx_volumes -v /opt/Volume/a:/opt/a -v /opt/Volume/b:/opt/b centos:6.9
[root@30c7325ff83f /]# [root@localhost ~]# 

启动两个nginx容器

[root@localhost ~]# docker container run -d --name nginx_server -p 8085:80 --volumes-from nginx_volumes nginx:latest 
d307fbe48293846a3112d27b927026af2b640096a14bf5715530c0a3f29394d5
[root@localhost ~]# docker container run -d --name nginx_server2 -p 8086:80 --volumes-from nginx_volumes nginx:latest 
f49180558986bf37214e3bbc374126e91d235026c956599cd6585f5664b3fd22

测试

连接容器查看

[root@localhost ~]# docker container exec -it nginx_server /bin/bash
root@d307fbe48293:/# ls /opt/
a  b
root@d307fbe48293:/# ls /opt/a
a.txt
root@d307fbe48293:/# ls /opt/b
b.txt
root@d307fbe48293:/# 

修改文件查看
在宿主机修改文件

[root@localhost ~]# echo hello > /opt/Volume/a/a.txt 
[root@localhost ~]# 

在nginx容器查看文件内容

[root@localhost ~]# docker container exec -it nginx_server /bin/bash
root@d307fbe48293:/# cat /opt/a/a.txt 
hello
root@d307fbe48293:/# 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值