マルチステージビルドでイメージを軽量化する
ビルド層とランタイム層を分けてイメージサイズを削減します。
「マルチステージビルドでイメージを軽量化する」はCoddyKit上の無料MLOps Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMLOps Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 MLOps Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Big Images Hurt
A bloated image is slow to push, pull, and start, and it carries more to patch. Multi-stage builds ship only what you actually need.
The Core Idea
You use one stage to build and a second clean stage to run. Build tools stay behind, so the final image stays small.
Name a Build Stage
Add AS builder to a FROM line to name a stage. You can reference that name later to pull files out of it.
FROM python:3.11 AS builderInstall Into the Builder
In the builder, install dependencies to a known prefix. The --user flag drops everything neatly under one copyable folder.
RUN pip install --user --no-cache-dir -r requirements.txtStart the Runtime Stage
Open a fresh, slim stage for what actually runs. It has none of the compilers or caches the builder needed.
FROM python:3.11-slimCopy Only the Artifacts
Use COPY --from=builder to lift just the installed packages into the runtime stage. The heavy build layer is left behind.
COPY --from=builder /root/.local /root/.localBring in Your Code
Copy your application source into the slim stage too. Now the final image holds your code plus only its runtime dependencies.
COPY . /appFix the Path
User-installed packages live under .local, so add that to PATH. Otherwise the runtime cannot find your installed commands.
ENV PATH=/root/.local/bin:$PATHOnly Last Stage Ships
Docker keeps only the final stage in your image. Everything in earlier stages is discarded unless you explicitly copy it.
Try Distroless
For the smallest, safest result, run on a distroless base with no shell or package manager. Less surface means fewer things to exploit.
Measure the Win
Check the size drop with docker images. Slimming from over a gigabyte to a couple hundred megabytes is common and worth it.
docker images model-apiQuick Check
Let us check what a multi-stage build keeps.
Recap
You split build from runtime, copied just the artifacts with COPY --from, fixed PATH, and shrank the image. Lean and fast. ✅
よくある質問
「マルチステージビルドでイメージを軽量化する」レッスンは無料ですか?
はい。「マルチステージビルドでイメージを軽量化する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、MLOps Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 MLOps Academyコースには全4レッスンが含まれています。
「マルチステージビルドでイメージを軽量化する」で何を学びますか?
ビルド層とランタイム層を分けてイメージサイズを削減します。 ブラウザで直接実行するハンズオンコードでMLOps Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
MLOps Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMLOps Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「マルチステージビルドでイメージを軽量化する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMLOps Academyレッスンでコードを書いて実行できますか?
はい。すべてのMLOps Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- モデルAPI用のDockerfileを書く
- マルチステージビルドでイメージを軽量化する
- 環境変数で設定を渡す
- コンテナをローカルで実行・テストする