0Pricing
MLOps Academy · บทเรียน

เขียน Dockerfile สำหรับ API ของโมเดล

ทำบริการ FastAPI ให้เป็นคอนเทนเนอร์ทีละขั้นตอน

เขียน Dockerfile สำหรับ API ของโมเดล เป็นบทเรียน MLOps Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน MLOps Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส MLOps Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why a Dockerfile

Your model API runs on your laptop but breaks elsewhere. A Dockerfile is a recipe that builds the exact same environment everywhere.

Pick a Base Image

Every Dockerfile starts FROM a base image. A slim Python image gives you the interpreter without a bloated operating system.

FROM python:3.11-slim

Set a Working Directory

The WORKDIR instruction picks the folder your commands run in. It is created if missing, so later paths stay clean and predictable.

WORKDIR /app

Copy Requirements First

Copy requirements.txt alone before your code. Docker caches this layer, so installs are skipped when only your app changes.

COPY requirements.txt .

Install Dependencies

Run pip install inside the image. The --no-cache-dir flag skips the pip cache and keeps the final image a little smaller.

RUN pip install --no-cache-dir -r requirements.txt

Copy Your Code

Now COPY the rest of your project in. Doing this after the install means code edits do not bust the dependency cache layer.

COPY . .

Document the Port

The EXPOSE instruction declares which port your API listens on. It is documentation for humans and tools, not an actual open.

EXPOSE 8000

Define the Start Command

The CMD sets what runs when the container starts. Here it launches Uvicorn to serve your FastAPI model app.

CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]

Bind to All Interfaces

Inside a container you must serve on 0.0.0.0, not 127.0.0.1. Otherwise the host cannot reach your model through the mapped port.

Build the Image

Turn the recipe into an image with docker build. The -t flag tags it with a name you can run and push later.

docker build -t model-api .

Layers Are Cached

Each instruction becomes a cached layer. Order them from least to most frequently changed so rebuilds stay fast.

Quick Check

Let us check the most cache-friendly build order.

Recap

You wrote a Dockerfile: base image, workdir, cached install, copy, expose, and a start command. Your model API now builds the same anywhere. 🐳

คำถามที่พบบ่อย

บทเรียน “เขียน Dockerfile สำหรับ API ของโมเดล” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “เขียน Dockerfile สำหรับ API ของโมเดล” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส MLOps Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส MLOps Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “เขียน Dockerfile สำหรับ API ของโมเดล”

ทำบริการ FastAPI ให้เป็นคอนเทนเนอร์ทีละขั้นตอน คุณปฏิบัติ MLOps Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน MLOps Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน MLOps Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “เขียน Dockerfile สำหรับ API ของโมเดล” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน MLOps Academy นี้ได้ไหม

ได้ บทเรียน MLOps Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เขียน Dockerfile สำหรับ API ของโมเดล
  2. ลดขนาดอิมเมจด้วยการสร้างหลายระยะ
  3. ส่งการตั้งค่าผ่านตัวแปรสภาพแวดล้อม
  4. รันและทดสอบคอนเทนเนอร์ภายในเครื่อง
← กลับไปที่ MLOps Academy