Docker绑定挂载使用手册

目录

目标

官方文档

绑定挂挂载(Bind mounts)

简介

基本创建方法

控制读写权限(默认有读写权限)

为什么绑定挂载不适合做数据库持久化

为什么绑定挂载更适合做热部署

临时挂载(tmpfs mounts)

简介

基本创建方法

验证临时挂载的生命周期

最佳实践

数据卷&绑定挂载&临时挂载的区别


目标

        掌握绑定挂载、临时挂载的常用命令。理解绑定挂载、数据卷、临时挂载之间的区别。


官方文档

Bind mounts


绑定挂挂载(Bind mounts)

简介

官方定义

Bind mounts have been around since the early days of Docker. Bind mounts have limited functionality compared to volumes. When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its absolute path on the host machine. By contrast, when you use a volume, a new directory is created within Docker's storage directory on the host machine, and Docker manages that directory's contents.

The file or directory does not need to exist on the Docker host already. It is created on demand if it does not yet exist. Bind mounts are very performant, but they rely on the host machine's filesystem having a specific directory structure available. If you are developing new Docker applications, consider using named volumes instead. You can't use Docker CLI commands to directly manage bind mounts.

官方示意图 

翻译

  1. 与数据卷相比,绑定挂在功能有限。
  2. 绑定挂载要用主机上的绝对目录引用,如果目录不存在,则会按需求创建。而使用数据卷时,不需要引用主机上的绝对目录,Docker会创建目录并管理这个目录中的内容。
  3. 绑定挂载性能高,但推荐优先考虑使用数据卷,因为绑定挂载依赖于主机文件系统中存在特定的目录结构(官方的示意图很好地体现了这一点)。
  4. Docker命令不能直接管理绑定挂载。

基本创建方法

方法一:用mount参数创建。前提条件:引用的主机目录必须存在,否则报错。

#source:主机目录
#target:容器目录
#type:挂载类型为绑定
docker run -d -p 8848:80 --name nginx --mount type=bind,source=/opt/data/,target=/usr/share/nginx/html nginx

方法二:用v参数创建。如果主机目录不存在则会自动创建目录。

#-v前面的目录表示主机目录,如果这个目录不存在则会自动创建。
#-v后面的目录表示容器目录
docker run -d -p 5601:80 --name nginx2 -v /opt/data/html:/usr/share/nginx/html nginx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值