Files
docker_practice/08_data/8.3_tmpfs.md
T
2026-03-28 18:24:46 -07:00

40 lines
1.5 KiB
Go
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.
## 8.3 tmpfs 挂载
`tmpfs` 挂载会把数据放在内存中,而不是写入容器可写层或数据卷。它只适用于 Linux 语义的容器环境,适合需要快速读写但不要求持久化的数据。
### 8.3.1 适用场景
- 临时缓存
- 会话数据
- 不希望落盘的敏感中间文件
### 8.3.2 基本用法
使用 `--mount` 语法(推荐):
```bash
$ docker run --mount type=tmpfs,destination=/run,tmpfs-size=67108864,tmpfs-mode=1770 nginx
```
也可以使用 `--tmpfs` 简写语法:
```bash
$ docker run --tmpfs /run:size=64m nginx
```
> **注意**:`--tmpfs` 更适合简单场景;如果你希望显式描述挂载点、大小和权限,`--mount type=tmpfs,...` 的可读性更好,也更便于后续维护。
### 8.3.3 注意事项
- 容器停止后,`tmpfs` 数据会丢失。
- `tmpfs` 占用宿主机内存,建议显式限制大小。
- 不适合需要持久化的数据。
- `tmpfs` 不适合多个容器共享同一份数据,也不适合当作跨重启的缓存层。
- 在内存压力较高时,部分数据可能受系统交换机制影响,因此不要把 `tmpfs` 当作绝对不会落盘的安全边界。
### 8.3.4 与 Volume / Bind Mount 对比
| 类型 | 数据位置 | 持久化 | 典型用途 |
|------|---------|-------|---------|
| Volume | Docker 管理目录 | 是 | 数据库、长期业务数据 |
| Bind Mount | 宿主机指定目录 | 是 | 开发联调、配置文件共享 |
| tmpfs | 内存 | 否 | 高速临时数据、敏感临时文件 |