0Pricing
Django Academy · درس

كتابة Dockerfile لـ Django

إنشاء صورة خفيفة متعددة الطبقات لتطبيقك

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

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

Why a Dockerfile

A Dockerfile is a recipe that builds your app into a portable image, so it runs the same on your laptop and any server. 🐳

Pick a Base Image

Start every Dockerfile with FROM, choosing a slim Python base so your image stays small and fast to ship.

FROM python:3.12-slim

Set the Work Directory

Use WORKDIR to set the folder where your code lives, so later commands run from a clean, predictable place.

WORKDIR /app

Tame Python Output

Two ENV flags help in containers: unbuffered logs and no .pyc files, keeping output instant and images tidy.

ENV PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1

Copy Requirements First

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

COPY requirements.txt .

Install Dependencies

Run pip install with --no-cache-dir so the package cache never bloats your final image.

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

Copy Your Project

Now COPY the rest of your project in. Doing this after the install keeps the cached dependency layer reusable.

COPY . .

Expose the Port

Use EXPOSE to document the port your app listens on. It is a hint to readers and tools, not a firewall.

EXPOSE 8000

Define the Start Command

End with CMD, the default command that launches your app when the container starts.

CMD ["gunicorn", "config.wsgi:application", "--bind", "0.0.0.0:8000"]

Add a .dockerignore

A .dockerignore keeps junk like venvs and .git out of the build context, making builds faster and images leaner.

.git
__pycache__
.venv
*.sqlite3

Build Your Image

Run docker build with a tag to turn your Dockerfile into a reusable, named image you can run anywhere.

docker build -t myapp:latest .

Quick Check

Think about Docker's layer cache and build order.

Recap

You built a Django Dockerfile: pick a slim base, set env, install deps before code for caching, then define CMD. One recipe, runs anywhere. 🎉

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

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

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

ماذا ستتعلم في «كتابة Dockerfile لـ Django»؟

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

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

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

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

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

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

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

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

  1. كتابة Dockerfile لـ Django
  2. docker-compose مع Postgres وRedis
  3. Entrypoints وMigrations وcollectstatic
  4. أفضل ممارسات صور الإنتاج
← العودة إلى Django Academy