# UnslothとDockerでLLMをファインチューニングする方法

ローカルトレーニングは依存関係の地獄や環境の破損により複雑になることがあります。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/jp/meru/fine-tuning-for-beginners/unsloth-requirements.md#system-requirements) （例：Windows）で動作します。

* 完全に含まれた依存関係によりシステムはクリーンに保たれます。root権限なしで安全に実行されます。
* ローカルでも、ノートブックが事前にインストールされた任意のプラットフォームでも使用できます。

{% hint style="success" %}
現在、Blackwellおよび50シリーズGPU向けに当社のメインDockerイメージを `unsloth/unsloth` 使用できます — 別個のイメージは不要です。
{% endhint %}

### ⚡ ステップバイステップチュートリアル

{% stepper %}
{% step %}
**DockerとNVIDIA Container Toolkitをインストールします。**

Dockerをインストール（via） [Linux](https://docs.docker.com/engine/install/) または [デスクトップ](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="/files/5989c34ec51a66dd4f33aeb7f57cf6f326998582" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**コンテナを実行します。**

[**`unsloth/unsloth`**](https://hub.docker.com/r/unsloth/unsloth) はUnslothの唯一のDockerイメージです。と [Blackwell](/docs/jp/burogu/fine-tuning-llms-with-blackwell-rtx-50-series-and-unsloth.md) および50シリーズGPUには、この同じイメージを使用してください — 別個のイメージは不要です。DGX Sparkを使用する場合は、当社の [DGXガイド](/docs/jp/burogu/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="/files/b3781c6f4fd1794d516751b93c1c81f9cf6f4580" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**Jupyter Labにアクセス**

に移動します [http://localhost:8888](http://localhost:8888/) そしてUnslothを開きます。

<figure><img src="/files/b45184ea982f9a1407e91fc7194762506733450e" alt="" width="563"><figcaption></figcaption></figure>

にアクセス `unsloth-notebooks` タブでUnslothのノートブックを確認できます。

<div><figure><img src="/files/8fb7fe073377987a2c530757d72dcae5d1e36d3a" alt=""><figcaption></figcaption></figure> <figure><img src="/files/8b318f86959d98f3a5b3a7b0677e06ca6d9f2a8e" alt=""><figcaption></figcaption></figure></div>
{% endstep %}

{% step %}
**Unslothでトレーニングを開始**

初めての場合は、当社のステップバイステップの [ファインチューニングガイド](/docs/jp/meru/fine-tuning-llms-guide.md), [RLガイド](/docs/jp/meru/reinforcement-learning-rl-guide.md) に従うか、または単に当社の事前作成された [ノートブック](/docs/jp/meru/unsloth-notebooks.md).

<figure><img src="/files/1670d0a900cbe632f74328308290afd4002572aa" 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" \
  -v $(pwd)/work:/workspace/work \
  --gpus all \
  unsloth/unsloth
```

#### -p 8000:8000 -p 2222:22 \\

SSHキーの設定

```bash
SSH鍵ペアを持っていない場合：
# 新しい鍵ペアを生成

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               | `unsloth`    |
| `Jupyter Labのパスワード`    | JUPYTER\_PORT                   | `8888`       |
| `コンテナ内のJupyter Labポート` | SSH\_KEY                        | `認証用のSSH公開鍵` |
| `なし`                   | USER\_PASSWORD `unsloth` のパスワード | `unsloth`    |

```bash
ユーザー（sudo）
```

* -p \<host\_port>:\<container\_port> `Jupyter Lab：`
* -p 8000:8888 `SSHアクセス：`

{% hint style="warning" %}
**-p 2222:22**重要
{% endhint %}

```bash
： コンテナ実行間で作業を保持するにはボリュームマウントを使用してください。
```

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

### **-v \<local\_folder>:\<container\_folder>**

* 🔒 セキュリティ注意事項 `unsloth` コンテナはデフォルトで非root
* 使用する `なし` ユーザーとして実行されます
* コンテナ内でのsudo操作には、公開鍵認証が必要です


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://unsloth.ai/docs/jp/burogu/how-to-fine-tune-llms-with-unsloth-and-docker.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
