# DockerでUnslothをインストール

依存関係がすべて事前インストールされたDockerコンテナを使って、すぐにインストールできる方法を学びましょう。セットアップは不要で、実行して学習を始めるだけです！

UnslothのDockerイメージ： [**`unsloth/unsloth`**](https://hub.docker.com/r/unsloth/unsloth)

{% hint style="success" %}
Unsloth Studioは、不要な再ダウンロードを避けるため、ノートブックやスクリプトと同じキャッシュを共有するようになりました。
{% endhint %}

### ⚡ クイックスタート

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

Dockerをインストール： [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イメージです。

```bash
docker run -d -e JUPYTER_PASSWORD="mypassword" \
  -p 8888:8888 -p 8000:8000 -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" \
  -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
```

### 🦥 なぜUnslothコンテナなのか？

* **信頼性**: 安定して保守されたパッケージバージョンを備えた厳選環境。圧縮サイズはわずか7 GB（他では10〜11 GB）
* **すぐに使える**: 事前インストール済みのノートブックが `/workspace/unsloth-notebooks/`
* **安全**: 非rootユーザーとして安全に実行
* **汎用**: すべてのトランスフォーマーベースのモデル（TTS、BERTなど）と互換

### **UnslothがGPUを検出していない、または使用していない**

DockerでモデルがGPUを特に使用していない場合は、次を試してください：

最新イメージを手動で取得：

```bash
 docker pull unsloth/unsloth:latest
```

* GPUアクセスを有効にしてコンテナを起動：
  * `docker run`: `--gpus all`
  * Docker Compose： `capabilities: [gpu]`
* Linuxでは、NVIDIA Container Toolkitがインストールされていることを確認してください。
* Windowsでは：
  * 次を確認してください `nvcc --version` が、以下に表示されるCUDAのバージョンと一致することを `nvidia-smi`
  * こちらに従ってください： <https://docs.docker.com/desktop/features/gpu/>

### ⚙️ 詳細設定

```bash
# SSHキーペアを生成
ssh-keygen -t rsa -b 4096 -f ~/.ssh/container_key

# コンテナに接続
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: 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/meru/install/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.
