Files
nexus/wiki/concepts/BindMount.md

49 lines
1.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "Bind Mount"
type: concept
tags: [docker, storage, development]
last_updated: 2026-04-17
---
## Aliases
- Bind Mount
- 绑定挂载
- Host Volume Mount
## Definition
Docker 中将宿主机的特定目录或文件直接映射到容器内部的一种挂载方式。与命名卷Named Volume不同绑定挂载使用宿主机上的绝对路径使容器内外的文件系统保持同步实现代码修改实时生效。
## Use Cases
- **开发环境**:代码文件频繁变更,绑定挂载实现宿主机编辑 → 容器即时生效的热重载
- **配置文件**:将 nginx.conf、.env 等配置文件挂载进容器,方便实时调整
- **日志收集**:将容器日志目录挂载到宿主机,实现日志持久化和集中管理
## Syntax
```yaml
# docker-compose.yml
services:
app:
volumes:
- /home/user/project/src:/app/src # 宿主机路径:容器路径
- ./config:/app/config # 相对路径(相对于 compose 文件位置)
```
## Trade-offs
| 优点 | 缺点 |
|------|------|
| 代码修改实时生效 | 依赖宿主机文件系统结构 |
| 无需数据迁移 | 权限问题UID/GID 不匹配)|
| 便于调试和热重载 | 生产环境不推荐使用 |
## Related Concepts
- [[Docker Volume]]:命名卷,适合生产环境的数据持久化
- [[Remote Development]]:远程开发中使用 Bind Mount 实现代码同步
- [[Docker Compose]]:通过 YAML 定义 Bind Mount 配置
## Related Entities
- [[Docker]]Bind Mount 的实现平台
- [[Trae]]:通过 Remote-SSH 连接远程服务器,编辑 Bind Mount 挂载的代码
## References
- [[Trae远程开发部署指南]] — 开发环境 docker-compose.yml 使用 Bind Mount 实现代码热重载