> For the complete documentation index, see [llms.txt](https://philipzheng.gitbook.io/docker_practice/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://philipzheng.gitbook.io/docker_practice/data_management/tmpfs.md).

# tmpfs 掛載

`tmpfs` 掛載會把資料放在記憶體中，而不是寫入容器可寫層或資料卷。它只適用於 Linux 語意的容器環境，適合需要快速讀寫但不要求持久化的資料。

## 適用場景

* 臨時快取
* 會話資料
* 不希望落盤的敏感中間檔案

## 基本用法

使用 `--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,...` 的可讀性更好，也更便於後續維護。

## 注意事項

* 容器停止後，`tmpfs` 資料會丟失。
* `tmpfs` 佔用宿主機記憶體，建議顯式限制大小。
* 不適合需要持久化的資料。
* `tmpfs` 不適合多個容器共享同一份資料，也不適合當作跨重啟的快取層。
* 在記憶體壓力較高時，部分資料可能受系統交換機制影響，因此不要把 `tmpfs` 當作絕對不會落盤的安全邊界。

## 與 Volume / Bind Mount 對比

| 類型         | 資料位置        | 持久化 | 典型用途          |
| ---------- | ----------- | --- | ------------- |
| Volume     | Docker 管理目錄 | 是   | 資料庫、長期業務資料    |
| Bind Mount | 宿主機指定目錄     | 是   | 開發聯調、設定檔案共享   |
| tmpfs      | 記憶體         | 否   | 高速臨時資料、敏感臨時檔案 |
