0Pricing
Flask Academy · درس

كتابة Dockerfile للإنتاج

أنشئ صورة حاوية صغيرة وقابلة لإعادة الإنتاج.

كتابة Dockerfile للإنتاج درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Containerize

A Docker image packs your code, Python, and every dependency into one unit that runs the same on any machine. No more works on my laptop. 🐳

The Dockerfile

A Dockerfile is a recipe. Each line is a step Docker runs to build your image, from base layer to final run command.

Pick a Slim Base

Start from an official python image. The slim variant keeps the image small while still giving you a working Python runtime.

FROM python:3.12-slim

Set a Working Directory

Use WORKDIR to choose where your code lives inside the image. Every later command runs relative to this folder.

WORKDIR /app

Copy Requirements First

Copy requirements.txt before your code. This lets Docker cache the install layer and skip it when only your code changes.

COPY requirements.txt .

Install Dependencies

Run pip install with no cache to keep the layer lean. This bakes Flask and Gunicorn right into the image.

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

Copy the Rest of the Code

Now COPY the rest of your project in. Because it comes after the install, edits to your code do not bust the dependency cache.

COPY . .

Document the Port

Use EXPOSE to declare which port your app listens on. It is documentation plus a hint for tooling that wires things up.

EXPOSE 8000

Define the Start Command

The CMD line is what runs when the container starts. Point it at Gunicorn so the container boots straight into your server.

CMD ["gunicorn", "--bind", "0.0.0.0:8000", "wsgi:app"]

Ignore the Junk

Add a .dockerignore file so caches, venvs, and git data never enter the image. Smaller builds are faster and safer.

__pycache__/
.venv/
.git/

Build and Run It

Build with docker build, then run the image and map a port. Your packaged Flask app is now portable to any host.

docker build -t myapp .
docker run -p 8000:8000 myapp

Quick Check

Why copy requirements.txt before the rest of your code?

Recap

Your Dockerfile builds from a slim base, installs deps, copies code, and launches Gunicorn. One image runs anywhere. Next: put Nginx in front. 🔧

الأسئلة الشائعة

هل درس «كتابة Dockerfile للإنتاج» مجاني؟

نعم — نص درس «كتابة Dockerfile للإنتاج» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.

ماذا ستتعلم في «كتابة Dockerfile للإنتاج»؟

أنشئ صورة حاوية صغيرة وقابلة لإعادة الإنتاج. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟

لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «كتابة Dockerfile للإنتاج»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟

نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. لماذا لا تستخدم خادم التطوير المضمّن
  2. تشغيل Flask باستخدام Gunicorn
  3. كتابة Dockerfile للإنتاج
  4. وضع Nginx في الواجهة واستخدام Compose
← العودة إلى Flask Academy