Add more content

This commit is contained in:
Baohua Yang
2026-01-30 16:48:39 -08:00
parent c58f61dbed
commit fec2e506d9
39 changed files with 8202 additions and 1063 deletions

View File

@@ -1,73 +1,284 @@
# 挂载主机目录
# 挂载主机目录Bind Mounts
## 挂载一个主机目录作为数据卷
## 什么是 Bind Mount
使用 `--mount` 标记可以指定挂载一个本地主机的目录到容器中去
Bind Mount绑定挂载**宿主机的目录或文件**直接挂载到容器中容器可以读写宿主机的文件系统
```bash
$ docker run -d -P \
--name web \
# -v /src/webapp:/usr/share/nginx/html \
--mount type=bind,source=/src/webapp,target=/usr/share/nginx/html \
nginx:alpine
```
宿主机 容器
┌─────────────────────┐ ┌─────────────────────┐
│ /home/user/code/ │ │ │
├── index.html │◄───────►│ /usr/share/nginx/
│ ├── style.css │ Bind │ html/ │
│ └── app.js │ Mount │ (同一份文件) │
└─────────────────────┘ └─────────────────────┘
```
上面的命令加载主机的 `/src/webapp` 目录到容器的 `/usr/share/nginx/html`目录这个功能在进行测试的时候十分方便比如用户可以放置一些程序到本地目录中来查看容器是否正常工作本地目录的路径必须是绝对路径以前使用 `-v` 参数时如果本地目录不存在 Docker 会自动为你创建一个文件夹现在使用 `--mount` 参数时如果本地目录不存在Docker 会报错
---
Docker 挂载主机目录的默认权限是 `读写`用户也可以通过增加 `readonly` 指定为 `只读`
## Bind Mount vs Volume
```bash
$ docker run -d -P \
--name web \
# -v /src/webapp:/usr/share/nginx/html:ro \
--mount type=bind,source=/src/webapp,target=/usr/share/nginx/html,readonly \
nginx:alpine
| 特性 | Bind Mount | Volume |
|------|------------|--------|
| **数据位置** | 宿主机任意路径 | Docker 管理的目录 |
| **路径指定** | 必须是绝对路径 | 卷名 |
| **可移植性** | 依赖宿主机路径 | 更好Docker 管理 |
| **性能** | 依赖宿主机文件系统 | 优化的存储驱动 |
| **适用场景** | 开发环境配置文件 | 生产数据持久化 |
| **备份** | 直接访问文件 | 需要通过 Docker |
### 选择建议
```
需求 推荐方案
─────────────────────────────────────────
开发时同步代码 → Bind Mount
持久化数据库数据 → Volume
共享配置文件 → Bind Mount
容器间共享数据 → Volume
备份方便 → Bind Mount直接访问
生产环境 → Volume
```
加了 `readonly` 之后就挂载为 `只读` 如果你在容器内 `/usr/share/nginx/html` 目录新建文件会显示如下错误
---
## 基本语法
### 使用 --mount推荐
```bash
/usr/share/nginx/html # touch new.txt
touch: new.txt: Read-only file system
$ docker run -d \
--mount type=bind,source=/宿主机路径,target=/容器路径 \
nginx
```
## 查看数据卷的具体信息
在主机里使用以下命令可以查看 `web` 容器的信息
### 使用 -v简写
```bash
$ docker inspect web
$ docker run -d \
-v /宿主机路径:/容器路径 \
nginx
```
`挂载主机目录` 的配置信息在 "Mounts" Key 下面
### 两种语法对比
| 特性 | --mount | -v |
|------|---------|-----|
| 语法 | 键值对更清晰 | 冒号分隔更简洁 |
| 路径不存在时 | 报错 | 自动创建目录 |
| 推荐程度 | 推荐 | 常用 |
---
## 使用场景
### 场景一开发环境代码同步
```bash
# 将本地代码目录挂载到容器
$ docker run -d \
-p 8080:80 \
--mount type=bind,source=$(pwd)/src,target=/usr/share/nginx/html \
nginx
# 修改本地文件,容器内立即生效(热更新)
$ echo "Hello" > src/index.html
# 浏览器刷新即可看到变化
```
### 场景二配置文件挂载
```bash
# 挂载自定义 nginx 配置
$ docker run -d \
--mount type=bind,source=/path/to/nginx.conf,target=/etc/nginx/nginx.conf,readonly \
nginx
```
### 场景三日志收集
```bash
# 将容器日志输出到宿主机目录
$ docker run -d \
--mount type=bind,source=/var/log/myapp,target=/app/logs \
myapp
```
### 场景四共享 SSH 密钥
```bash
# 挂载 SSH 密钥(只读)
$ docker run --rm -it \
--mount type=bind,source=$HOME/.ssh,target=/root/.ssh,readonly \
alpine ssh user@remote
```
---
## 只读挂载
防止容器修改宿主机文件
```bash
# --mount 语法
$ docker run -d \
--mount type=bind,source=/config,target=/app/config,readonly \
myapp
# -v 语法
$ docker run -d \
-v /config:/app/config:ro \
myapp
```
容器内尝试写入会报错
```bash
$ touch /app/config/new.txt
touch: /app/config/new.txt: Read-only file system
```
---
## 挂载单个文件
```bash
# 挂载 bash 历史记录
$ docker run --rm -it \
--mount type=bind,source=$HOME/.bash_history,target=/root/.bash_history \
ubuntu bash
# 挂载自定义配置文件
$ docker run -d \
--mount type=bind,source=/path/to/my.cnf,target=/etc/mysql/my.cnf \
mysql
```
> **注意**挂载单个文件时如果宿主机上的文件被编辑器替换而非原地修改容器内仍是旧文件的 inode建议重启容器或挂载目录
---
## 查看挂载信息
```bash
$ docker inspect mycontainer --format '{{json .Mounts}}' | jq
```
输出
```json
"Mounts": [
{
"Type": "bind",
"Source": "/src/webapp",
"Destination": "/usr/share/nginx/html",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
[
{
"Type": "bind",
"Source": "/home/user/code",
"Destination": "/app",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
]
```
## 挂载一个本地主机文件作为数据卷
| 字段 | 说明 |
|------|------|
| `Type` | 挂载类型bind |
| `Source` | 宿主机路径 |
| `Destination` | 容器内路径 |
| `RW` | 是否可读写 |
| `Propagation` | 挂载传播模式 |
`--mount` 标记也可以从主机挂载单个文件到容器中
---
## 常见问题
### Q: 路径不存在报错
```bash
$ docker run --rm -it \
# -v $HOME/.bash_history:/root/.bash_history \
--mount type=bind,source=$HOME/.bash_history,target=/root/.bash_history \
ubuntu:24.04 \
bash
root@2affd44b4667:/# history
1 ls
2 diskutil list
$ docker run --mount type=bind,source=/not/exist,target=/app nginx
docker: Error response from daemon: invalid mount config for type "bind":
bind source path does not exist: /not/exist
```
这样就可以记录在容器输入过的命令了
**解决**确保源路径存在或改用 `-v`会自动创建
### Q: 权限问题
容器内用户可能无权访问挂载的文件
```bash
# 方法1确保宿主机文件权限允许容器用户访问
$ chmod -R 755 /path/to/data
# 方法2以 root 运行容器
$ docker run -u root ...
# 方法3使用相同的 UID
$ docker run -u $(id -u):$(id -g) ...
```
### Q: macOS/Windows 性能问题
Docker Desktop Bind Mount 性能较差需要跨文件系统同步
```bash
# 使用 :cached 或 :delegated 提高性能macOS
$ docker run -v /host/path:/container/path:cached myapp
```
| 选项 | 说明 |
|------|------|
| `:cached` | 宿主机权威容器读取可能延迟 |
| `:delegated` | 容器权威宿主机读取可能延迟 |
| `:consistent` | 默认完全一致最慢 |
---
## 最佳实践
### 1. 开发环境使用 Bind Mount
```bash
# 代码热更新
$ docker run -v $(pwd):/app -p 3000:3000 node npm run dev
```
### 2. 生产环境使用 Volume
```bash
# 数据持久化
$ docker run -v mysql_data:/var/lib/mysql mysql
```
### 3. 配置文件使用只读挂载
```bash
$ docker run -v /config/nginx.conf:/etc/nginx/nginx.conf:ro nginx
```
### 4. 注意路径安全
```bash
# ❌ 危险:挂载根目录或敏感目录
$ docker run -v /:/host ...
# ✅ 只挂载必要的目录
$ docker run -v /app/data:/data ...
```
---
## 本章小结
| 要点 | 说明 |
|------|------|
| **作用** | 将宿主机目录挂载到容器 |
| **语法** | `-v /宿主机:/容器` `--mount type=bind,...` |
| **只读** | 添加 `readonly` `:ro` |
| **适用场景** | 开发环境配置文件日志 |
| **vs Volume** | Bind 更灵活Volume 更适合生产 |
## 延伸阅读
- [数据卷](volume.md)Docker 管理的持久化存储
- [tmpfs 挂载](tmpfs.md)内存临时存储
- [Compose 数据管理](../compose/compose_file.md)Compose 中的挂载配置