كتابة Dockerfile لواجهة برمجة نموذج
حوّل خدمة FastAPI إلى حاوية خطوةً بخطوة.
كتابة Dockerfile لواجهة برمجة نموذج درس مجاني في MLOps Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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-slimSet 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 /appCopy 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.txtCopy 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 8000Define 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 لواجهة برمجة نموذج» مجاني؟
نعم — نص درس «كتابة Dockerfile لواجهة برمجة نموذج» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة MLOps Academy، انتقل إلى CoddyKit PRO. تتضمن دورة MLOps Academy 4 دروس في المجموع.
ماذا ستتعلم في «كتابة Dockerfile لواجهة برمجة نموذج»؟
حوّل خدمة FastAPI إلى حاوية خطوةً بخطوة. تتمرن على MLOps Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ MLOps Academy؟
لا تُشترط خبرة سابقة. MLOps Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «كتابة Dockerfile لواجهة برمجة نموذج»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس MLOps Academy هذا؟
نعم. كل درس في MLOps Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- كتابة Dockerfile لواجهة برمجة نموذج
- تصغير الصور باستخدام عمليات البناء متعددة المراحل
- تمرير الإعدادات عبر متغيرات البيئة
- تشغيل الحاوية واختبارها محلياً