ลดขนาดอิมเมจด้วยการสร้างหลายระยะ
ลดขนาดอิมเมจโดยแยกเลเยอร์การสร้างออกจากเลเยอร์รันไทม์
ลดขนาดอิมเมจด้วยการสร้างหลายระยะ เป็นบทเรียน MLOps Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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. ✅
คำถามที่พบบ่อย
บทเรียน “ลดขนาดอิมเมจด้วยการสร้างหลายระยะ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ลดขนาดอิมเมจด้วยการสร้างหลายระยะ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส MLOps Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส MLOps Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ลดขนาดอิมเมจด้วยการสร้างหลายระยะ”
ลดขนาดอิมเมจโดยแยกเลเยอร์การสร้างออกจากเลเยอร์รันไทม์ คุณปฏิบัติ MLOps Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน MLOps Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน MLOps Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ลดขนาดอิมเมจด้วยการสร้างหลายระยะ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน MLOps Academy นี้ได้ไหม
ได้ บทเรียน MLOps Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เขียน Dockerfile สำหรับ API ของโมเดล
- ลดขนาดอิมเมจด้วยการสร้างหลายระยะ
- ส่งการตั้งค่าผ่านตัวแปรสภาพแวดล้อม
- รันและทดสอบคอนเทนเนอร์ภายในเครื่อง