Docker容器数据持久化

Docker容器数据持久化

Docker容器是一种轻量级的虚拟化技术,它可以将应用程序及其依赖项打包在一起,形成一个可移植的镜像。这些镜像可以部署到不同的主机上,并且可以随时启动、停止、删除。

然而,由于容器是临时性的,一旦容器被停止或删除,其中的数据也将被删除。这对于一些需要长期保存数据的应用程序来说是不可接受的。因此,将数据持久化到容器外部是非常必要的。

数据持久化有几种方式:

  1. 挂载宿主机的目录到容器中,这样容器中的数据可以被保存到宿主机上,即使容器被删除或停止,数据也可以保持不变。

  1. 使用Docker数据卷,数据卷是一个可以跨容器共享和重用的特殊目录,可以将容器中的数据保存到数据卷中,并且数据卷可以与其他容器共享。

  1. 使用外部数据存储,如网络存储或云存储服务,将容器中的数据保存到这些存储中,可以使数据更加安全和可靠。

  1. 挂载本地目录

创建本地目录
# mkdir /opt/wwwroot
向本地目录中添加index.html文件
# echo 'hello world' > /opt/wwwroot/index.html
运行web1容器,把/opt/wwwroot目录挂载到/usr/share/nginx/html目录中
# docker run -d --name web1 -v /opt/wwwroot/:/usr/share/nginx/html/ nginx:latest
查看容器IP地址
# docker inspect web1 | grep IPAddress

......
 "IPAddress": "xxx.xx.x.x",
 ......
使用curl命令访问容器
# curl http://xxx.xx.x.x
hello world
  1. 使用Docker volume数据卷

创建数据卷

创建一个名称为nginx-vol的数据卷
# docker volume create nginx-vol
nginx-vol
确认数据卷创建后的位置
# ls /var/lib/docker/volumes/
backingFsBlockDev  metadata.db  nginx-vol
查看已经创建数据卷
# docker volume ls
DRIVER    VOLUME NAME
local     nginx-vol
查看数据卷详细信息
# docker volume inspect nginx-vol
[
    {
        "CreatedAt": "2023-03-03T11:25:16+08:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/nginx-vol/_data",
        "Name": "nginx-vol",
        "Options": {},
        "Scope": "local"
    }
]

使用数据卷

运行web2容器,使用--mount选项,实现数据卷挂载
# docker run -d --name web2 --mount src=nginx-vol,dst=/usr/share/nginx/html nginx:latest

或者

运行web2容器,使用-v选项,实现数据卷挂载
# docker run -d --name web2 -v nginx-vol:/usr/share/nginx/html/ nginx:latest
查看容器运行后数据卷中文件或子目录
# ls /var/lib/docker/volumes/nginx-vol/_data/
50x.html  index.html
使用curl命令访问容器
# curl http://xxx.xx.x.x
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
修改index.html文件内容
# echo "web2" > /var/lib/docker/volumes/nginx-vol/_data/index.html
再次使用curl命令访问容器
# curl http://xxx.xx.x.x
web2
  1. bind mounts

bind mounts和volume的区别在于,在容器启动以后,volume会容器中挂载的文件同步到本地目录中,而bind mounts不会。

创建用于容器挂载的目录web3root
# mkdir /opt/web3root
运行web3容器并使用bind mount方法实现本地任意目录挂载
# docker run -d --name web3 --mount type=bind,src=/opt/web3root,dst=/usr/share/nginx/html nginx:latest
查看已挂载目录,里面没有任何数据
# ls /opt/web3root/
添加内容至/opt/web3root/index.html中
# echo "web3" > /opt/web3root/index.html
使用curl命令访问容器
# curl http://xxx.xx.x.x
web3
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

像鸟一样菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值