> ## Documentation Index
> Fetch the complete documentation index at: https://docs.morph-data.io/llms.txt
> Use this file to discover all available pages before exploring further.

# 実行環境のカスタマイズ

Morph ではプロジェクト内の`Dockerfile`を編集することで、Morph Cloud 上での実行環境のカスタマイズを実施することができます。

## Dockerfile の基本構成

`morph new`コマンドで初期生成される Dockerfile は以下のような構成になっています。

<Warning>
  `MORPH_TASK_ROOT`の予約語は、`pip
      install`を正常に動作させるために必要な環境変数です。特別な理由がない限りは変更しないでください。
</Warning>

```Dockerfile theme={"dark"}
# Base image for Morph Cloud
FROM public.ecr.aws/i1l4z0u0/morph-data:python3.9

# Set working directory
WORKDIR /var/task

# Install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt --target "${MORPH_TASK_ROOT}"

# Copy source code and dependencies
COPY . .

# Command to run the Lambda function
CMD ["python", ".morph/core/morph/api/app.py"]
```

## パッケージの追加

Morph Cloud のベースイメージでは yum もしくは microdnf を用いてパッケージを管理します。必要なパッケージを RUN コマンドで追加することができます。

* ベースイメージは Debian Linux をベースに作成されています。パッケージマネージャーとして`apt-get`コマンドを利用できます。

例えば、以下のように`Dockerfile`に`apt-get`コマンドを追加することで、`curl`パッケージをインストールすることができます。

<Note>
  イメージサイズ削減のために、パッケージの追加後には`apt-get clean`
  コマンドを実行して不要なパッケージを削除することをお勧めします。
</Note>

```Dockerfile theme={"dark"}
# Base image for Morph Cloud
FROM public.ecr.aws/i1l4z0u0/morph-data:python3.9

# Set working directory
WORKDIR /var/task

# Install OS-level dependencies
RUN apt-get update && apt-get install -y \
    curl \
    && apt-get clean

-- 省略 --
```

# Python バージョンの変更

Morph では、`morph new` コマンドを利用してプロジェクトを作成する際、利用する Python のバージョンを 3.9, 3.10, 3.11, 3.12 から選択できます。

ここで選択したバージョンは、デプロイしたアプリケーションの実行環境にも反映されるよう、初期生成ファイルが設定されます。

```bash theme={"dark"}
morph new my_project
# 対話的にPythonバージョンを選択
```

<Note>
  選択したPythonバージョンは、プロジェクト内の設定ファイル（Dockerfileなど）にも反映されるため、Dockerfileをカスタマイズします。
</Note>

## デプロイ環境の Python バージョンを後から変更する

デプロイした環境で使用する Python のバージョンを後から変更したい場合は、プロジェクト内にある Dockerfile のベースイメージのタグを編集し、再度デプロイを行います。

### 例: Python3.12 のイメージを使う場合

```docker theme={"dark"}
FROM public.ecr.aws/i1l4z0u0/morph-cloud:python3.12

-- 省略 --
```

上記のように python3.9 から python3.10、python3.11、python3.12 など、任意のバージョンに変更可能です。
