uvのDockerfile最適化

uvによるPython開発で、Dockerイメージを最適化するために、コマンドの詳細を分析する

Docker with uv

最近の開発で、Pythonのパッケージ管理ツールを uv に切り替えて、それに伴うDockerイメージの最適化方法を知りたいです。

人気のあるレポジトリ FastAPIテンプレート にあるDockerfileを勉強材料にしました。

これからステップ・バイ・ステップで、Dockerfileの分析を行います。

1
2
FROM python:3.10
ENV PYTHONUNBUFFERED=1

PYTHONUNBUFFEREDを空以外の値に設定すると、Python標準出力stdout・標準エラーstderrストリームのバッファリングを行わず、 すぐにターミナルに書き出される。

これはなにかのエラーになるとき、デバッグしやすくなります。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
WORKDIR /app/

# Install uv
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#installing-uv
COPY --from=ghcr.io/astral-sh/uv:0.5.11 /uv /uvx /bin/

# Place executables in the environment at the front of the path
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#using-the-environment
ENV PATH="/app/.venv/bin:$PATH"

# Compile bytecode
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#compiling-bytecode
ENV UV_COMPILE_BYTECODE=1

UV_COMPILE_BYTECODEを設定することにより、パッケージをインストールする時に、.pyをバイトコード.pycファイルにビルドされる。

起動時間を短縮することができそうです。

1
2
3
# uv Cache
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#caching
ENV UV_LINK_MODE=copy

公式サイトにより、この設定はハードリンクが使えない場合にuvが警告しないようにするものです。

1
2
3
4
5
6
# Install dependencies
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#intermediate-layers
RUN --mount=type=cache,target=/root/.cache/uv \
    --mount=type=bind,source=uv.lock,target=uv.lock \
    --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
    uv sync --frozen --no-install-project

Dockerのキャッシュを利用するため、uv syncを実行するとき、パッケージのみインストールし、プロジェクトファイルを無視する。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
ENV PYTHONPATH=/app

COPY ./scripts /app/scripts

COPY ./pyproject.toml ./uv.lock ./alembic.ini /app/

COPY ./app /app/app
COPY ./tests /app/tests

# Sync the project
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#intermediate-layers
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync

CMD ["fastapi", "run", "--workers", "4", "app/main.py"]

すべてのパッケージがインストールされた後で、プロジェクト関連ファイルをコピーし、再度uv syncで同期する。

最後の部分をカスタマイズしたら、自分のプロジェクトに利用することができます。

Built with Hugo
Theme Stack designed by Jimmy