> For the complete documentation index, see [llms.txt](https://unsloth.ai/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://unsloth.ai/docs/zh/bo-ke/how-to-fine-tune-llms-with-unsloth-and-docker.md).

# 如何使用 Unsloth 和 Docker 对 LLM 进行微调

了解如何使用 Unsloth 的 Docker 镜像对 LLM 进行微调，或进行强化学习（RL）。

本地训练可能很复杂，因为依赖地狱或环境破坏。Unsloth 的 [Docker 镜像](https://hub.docker.com/r/unsloth/unsloth) 可以绕过这些问题。无需任何设置：拉取并运行镜像，然后开始训练。

* **Unsloth 官方 Docker 镜像：** [**`unsloth/unsloth`**](https://hub.docker.com/r/unsloth/unsloth)

**为什么使用 Unsloth 和 Docker？**

Unsloth 的 Docker 镜像稳定、最新，并可在 [受支持的环境](/docs/zh/kuai-su-kai-shi/fine-tuning-for-beginners/unsloth-requirements.md#system-requirements) （如 Windows）上运行。

* 完全封装的依赖项可保持系统整洁。无需 root 即可安全运行。
* 可本地使用，或在任何预装笔记本的平台注册使用。

{% hint style="success" %}
你现在可以使用我们的主 Docker 镜像 `unsloth/unsloth` 来支持 Blackwell 和 50 系列 GPU——无需单独的镜像。
{% endhint %}

### ⚡ 分步教程

{% stepper %}
{% step %}
**安装 Docker 和 NVIDIA Container Toolkit。**

通过以下方式安装 Docker： [Linux](https://docs.docker.com/engine/install/) 或 [Desktop](https://docs.docker.com/desktop/) （其他）。\
然后安装 [NVIDIA Container Toolkit](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html#installation):

<pre class="language-bash"><code class="lang-bash"><strong>export NVIDIA_CONTAINER_TOOLKIT_VERSION=1.17.8-1
</strong>sudo apt-get update &#x26;&#x26; sudo apt-get install -y \
  nvidia-container-toolkit=${NVIDIA_CONTAINER_TOOLKIT_VERSION} \
  nvidia-container-toolkit-base=${NVIDIA_CONTAINER_TOOLKIT_VERSION} \
  libnvidia-container-tools=${NVIDIA_CONTAINER_TOOLKIT_VERSION} \
  libnvidia-container1=${NVIDIA_CONTAINER_TOOLKIT_VERSION}
</code></pre>

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-41cae231ed4761f844ce9836e03b17aabd7c803c%2Fnvidia%20toolkit.png?alt=media" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**运行容器。**

[**`unsloth/unsloth`**](https://hub.docker.com/r/unsloth/unsloth) 是 Unsloth 唯一的 Docker 镜像。对于 [Blackwell](/docs/zh/bo-ke/fine-tuning-llms-with-blackwell-rtx-50-series-and-unsloth.md) 和 50 系列 GPU，请使用同一个镜像——无需单独的镜像。如果使用 DGX Spark，你需要遵循我们的 [DGX 指南](/docs/zh/bo-ke/fine-tuning-llms-with-nvidia-dgx-spark-and-unsloth.md).

```bash
docker run -d -e JUPYTER_PASSWORD="mypassword" \
  -p 8888:8888 -p 2222:22 \
  -v $(pwd)/work:/workspace/work \
  --gpus all \
  unsloth/unsloth
```

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-2b50d78c5d54eaf189c0a40d46c405585ea23082%2Fdocker%20run.png?alt=media" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**访问 Jupyter Lab**

前往 [http://localhost:8888](http://localhost:8888/) 并打开 Unsloth。

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-828df0a668fd94025c1193c24a7f09c1d58dcbd8%2Fjupyter.png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

访问 `unsloth-notebooks` 标签页以查看 Unsloth 笔记本。

<div><figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-e7a3f620a3ec5bff335632ff9b0cb422f76528a1%2FScreenshot_from_2025-09-30_21-38-15.png?alt=media" alt=""><figcaption></figcaption></figure> <figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-531882c33eb96dec24e2d7673471d6a3928a3951%2FScreenshot_from_2025-09-30_21-39-41.png?alt=media" alt=""><figcaption></figcaption></figure></div>
{% endstep %}

{% step %}
**开始使用 Unsloth 训练**

如果你是新手，请按照我们的分步 [微调指南](/docs/zh/kuai-su-kai-shi/fine-tuning-llms-guide.md), [RL 指南](/docs/zh/kuai-su-kai-shi/reinforcement-learning-rl-guide.md) ，或者 պարզապես保存/复制我们任何预制的 [笔记本](/docs/zh/kuai-su-kai-shi/unsloth-notebooks.md).

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-665f900b008991ddcd8fdabb773b292de3c41e72%2FScreenshot_from_2025-09-30_21-40-29.png?alt=media" alt=""><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

#### 📂 容器结构

* `/workspace/work/` — 你挂载的工作目录
* `/workspace/unsloth-notebooks/` — 微调示例笔记本
* `/home/unsloth/` — 用户主目录

### 📖 使用示例

#### 完整示例

```bash
docker run -d -e JUPYTER_PORT=8000 \
  -e JUPYTER_PASSWORD="mypassword" \
  -e "SSH_KEY=$(cat ~/.ssh/container_key.pub)" \
  -e USER_PASSWORD="unsloth2024" \
  -p 8000:8000 -p 2222:22 \
  -v $(pwd)/work:/workspace/work \
  --gpus all \
  unsloth/unsloth
```

#### 设置 SSH 密钥

如果你没有 SSH 密钥对：

```bash
# 生成新的密钥对
ssh-keygen -t rsa -b 4096 -f ~/.ssh/container_key

# 在 docker run 中使用公钥
-e "SSH_KEY=$(cat ~/.ssh/container_key.pub)"

# 通过 SSH 连接
ssh -i ~/.ssh/container_key -p 2222 unsloth@localhost
```

### ⚙️ 高级设置

| 变量                 | 描述                      | 默认        |
| ------------------ | ----------------------- | --------- |
| `JUPYTER_PASSWORD` | Jupyter Lab 密码          | `unsloth` |
| `JUPYTER_PORT`     | 容器内的 Jupyter Lab 端口     | `8888`    |
| `SSH_KEY`          | 用于身份验证的 SSH 公钥          | `无`       |
| `USER_PASSWORD`    | 密码用于 `unsloth` 用户（sudo） | `unsloth` |

```bash
-p <host_port>:<container_port>
```

* Jupyter Lab： `-p 8000:8888`
* SSH 访问： `-p 2222:22`

{% hint style="warning" %}
**重要**：使用卷挂载，以便在容器多次运行之间保留你的工作。
{% endhint %}

```bash
-v <local_folder>:<container_folder>
```

```bash
docker run -d -e JUPYTER_PORT=8000 \
  -e JUPYTER_PASSWORD="mypassword" \
  -e "SSH_KEY=$(cat ~/.ssh/container_key.pub)" \
  -e USER_PASSWORD="unsloth2024" \
  -p 8000:8000 -p 2222:22 \
  -v $(pwd)/work:/workspace/work \
  --gpus all \
  unsloth/unsloth
```

### **🔒 安全说明**

* 容器默认以非 root `unsloth` 用户运行
* 使用 `USER_PASSWORD` 用于在容器内执行 sudo 操作
* SSH 访问需要公钥认证


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://unsloth.ai/docs/zh/bo-ke/how-to-fine-tune-llms-with-unsloth-and-docker.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
