0Pricing
Docker & Kubernetes for Developers · درس

البناء متعدد المراحل لصور إنتاجية خفيفة

أتقن ملفات Dockerfile متعددة المراحل لتجميع تطبيقك أو حزمه في مرحلة بناء، ثم انسخ العناصر الناتجة فقط إلى صورة تشغيل صغيرة

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

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

The Fat Image Problem

A single-stage build often ships compilers, dev dependencies, and source code into production. The result is a large, slower-to-pull, larger-attack-surface image.

What Is a Multi-Stage Build

A multi-stage build uses several FROM statements in one Dockerfile. Earlier stages build artifacts; a final lean stage copies only what it needs to run.

A Go Multi-Stage Example

Compile in a Go image, then ship a tiny final image with just the binary.

FROM golang:1.22 AS build
WORKDIR /src
COPY . .
RUN go build -o app .

FROM alpine:3.20
COPY --from=build /src/app /app
ENTRYPOINT ["/app"]

Copying From a Named Stage

The key line is COPY --from=build. It pulls the compiled app binary out of the build stage, leaving the Go toolchain behind.

A Node.js Multi-Stage Example

Install and build with dev deps, then copy only the production output.

FROM node:20 AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:20-slim
WORKDIR /app
COPY --from=build /app/dist ./dist
COPY package*.json ./
RUN npm ci --omit=dev
CMD ["node", "dist/index.js"]

Choosing a Slim Base

Final stages should use minimal bases like alpine, -slim, or distroless. Smaller bases mean fewer packages and fewer vulnerabilities.

Distroless Images

Google distroless images contain only your app and its runtime, no shell or package manager. They are very small and hard to exploit interactively.

FROM gcr.io/distroless/static-debian12
COPY --from=build /src/app /app
ENTRYPOINT ["/app"]

Targeting a Specific Stage

You can build only up to a named stage with --target, useful for running tests in CI.

docker build --target build -t myapp:test .

Caching Dependencies

Copy dependency manifests before the rest of the source. Then dependency installation is cached and only re-runs when the manifest changes.

COPY package*.json ./
RUN npm ci
COPY . .

Measuring the Win

Compare sizes before and after. Multi-stage builds commonly shrink images from hundreds of MB to tens of MB.

docker images myapp

Combining With .dockerignore

Add a .dockerignore so build context excludes node_modules, .git, and secrets, speeding builds and avoiding leaking files into images.

node_modules
.git
*.env

Quick Check

Test what you have learned.

Recap

You learned to split Dockerfiles into build and runtime stages, copy only artifacts with --from, pick slim or distroless bases, target stages, and combine with .dockerignore for lean production images.

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

هل درس «البناء متعدد المراحل لصور إنتاجية خفيفة» مجاني؟

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

ماذا ستتعلم في «البناء متعدد المراحل لصور إنتاجية خفيفة»؟

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

هل أحتاج إلى خبرة سابقة لأبدأ Docker & Kubernetes for Developers؟

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

كم من الوقت يستغرق درس «البناء متعدد المراحل لصور إنتاجية خفيفة»؟

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

هل يمكنني كتابة وتشغيل أكواد في درس Docker & Kubernetes for Developers هذا؟

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

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

  1. تحويل تطبيق ويب إلى حاويات
  2. تحسين صور Docker
  3. أفضل ممارسات الأمان والإنتاج
  4. البناء متعدد المراحل لصور إنتاجية خفيفة
← العودة إلى Docker & Kubernetes for Developers