NestJS Enterprise Backend APIs · درس

الحاويات وKubernetes

حوّل تطبيق NestJS إلى حاوية باستخدام Docker، وتعلّم استراتيجيات النشر الأساسية باستخدام Kubernetes للاستضافة القابلة للتوسع.

الدرس 5 من 612 خطوة

الحاويات وKubernetes درس مجاني في NestJS Enterprise Backend APIs على CoddyKit. هذا هو الدرس 5 من أصل 6. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في NestJS Enterprise Backend APIs، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة NestJS Enterprise Backend APIs 6 دروس في المجموع.

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

Why Containerize Your App?

When building server applications like those with NestJS, consistency across environments is key. This is where containerization with Docker comes in.

Docker packages your application and all its dependencies into a single, isolated unit called a container. This ensures your app runs the same way everywhere.

  • Consistency: Works the same on dev, staging, prod.
  • Isolation: Prevents conflicts between apps.
  • Portability: Easily move apps between hosts.

Docker: Images & Containers

Let's understand the two core concepts in Docker:

  • A Docker Image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries, and settings. Think of it as a blueprint or a template.
  • A Docker Container is a runnable instance of an image. You can create, start, stop, move, or delete a container. Multiple containers can run from the same image.

Images are stored in registries like Docker Hub.

Crafting Your NestJS Dockerfile

A Dockerfile is a text file that contains all the commands a user could call on the command line to assemble an image. Here’s a basic one for a NestJS app:

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["node", "dist/main"]

  • FROM: Base image.
  • WORKDIR: Sets working directory.
  • COPY: Copies files.
  • RUN: Executes commands.
  • EXPOSE: Informs Docker that the container listens on the specified network ports at runtime.
  • CMD: Default command to execute.

Building Your Docker Image

Once you have your Dockerfile in your NestJS project's root, you can build an image from it. The docker build command processes the Dockerfile and creates a Docker image.

Open your terminal in the project root and run:

docker build -t my-nestjs-app .

  • -t my-nestjs-app: Tags the image with a name (my-nestjs-app).
  • .: Specifies the build context (current directory).

This command will execute each step in your Dockerfile, creating layers for your image.

Running Your NestJS Container

After building the image, you can run it as a container. The docker run command creates a new container and starts it.

Try running your NestJS app in a container:

docker run -p 3000:3000 my-nestjs-app

  • -p 3000:3000: Maps port 3000 on your host machine to port 3000 inside the container. This lets you access your app via http://localhost:3000.
  • my-nestjs-app: The name of the image you want to run.

Your NestJS application should now be running inside a Docker container!

Docker Compose for Local Dev

For applications with multiple services (like a NestJS backend and a PostgreSQL database), Docker Compose simplifies local development. It allows you to define and run multi-container Docker applications.

A docker-compose.yml file looks like this:

version: '3.8'
services:
api:
build: .
ports:
- '3000:3000'
depends_on:
- db
db:
image: postgres:13
environment:
POSTGRES_DB: nest
POSTGRES_USER: user
POSTGRES_PASSWORD: pass

Run with: docker-compose up

Kubernetes: Orchestrating Containers

While Docker is great for packaging and running individual containers, managing many containers across multiple servers (especially in production) becomes complex. This is where Kubernetes (K8s) comes in.

Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications.

  • Orchestration: Manages container lifecycle.
  • Scaling: Automatically adjusts resources.
  • Self-healing: Recovers from failures.

K8s Core: Pods

The smallest deployable unit in Kubernetes is a Pod. A Pod represents a single instance of a running process in your cluster. It can contain one or more containers (e.g., your NestJS app container and a sidecar logging container).

All containers in a Pod share the same network namespace, IP address, and storage volumes. This makes communication between them very efficient.

Example Pod definition:

apiVersion: v1
kind: Pod
metadata:
name: nestjs-pod
spec:
containers:
- name: nestjs-app
image: my-nestjs-app:latest
ports:
- containerPort: 3000

K8s Core: Deployments

A Deployment in Kubernetes provides declarative updates for Pods and ReplicaSets. You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state.

Deployments are used to:

  • Create ReplicaSets to bring up Pods.
  • Roll out new versions of your application.
  • Roll back to an earlier Deployment revision.
  • Scale your application up or down.

Example Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
name: nestjs-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nestjs-app
template:
metadata:
labels:
app: nestjs-app
spec:
containers:
- name: nestjs-app
image: my-nestjs-app:latest
ports:
- containerPort: 3000

K8s Core: Services

While Deployments manage Pods, Services in Kubernetes define a logical set of Pods and a policy by which to access them. Services enable network access to a set of Pods.

Pods are ephemeral (they can be created/destroyed), so their IP addresses change. A Service provides a stable IP address and DNS name, acting as a load balancer for the Pods it targets.

Example Service:

apiVersion: v1
kind: Service
metadata:
name: nestjs-service
spec:
selector:
app: nestjs-app
ports:
- protocol: TCP
port: 80
targetPort: 3000
type: LoadBalancer

This exposes your NestJS app externally.

Docker & K8s Knowledge Check

Which of the following is the correct command to build a Docker image from a Dockerfile located in the current directory, tagging it as my-app?

Recap: Dockerization & K8s

Great job! In this lesson, you've learned to containerize your NestJS application using Docker and explore basic deployment strategies with Kubernetes.

  • Docker helps package your app into consistent, isolated containers.
  • You learned to create Dockerfiles, build images, and run containers.
  • Docker Compose aids in local multi-service development.
  • Kubernetes orchestrates containers at scale, using Pods, Deployments, and Services.

These tools are fundamental for modern, scalable backend development!

البدء مجانًا

تعلم TypeScript مع معلم ذكاء اصطناعي — مجانًا

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

الدورات
20
الدروس
76

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

هل درس «الحاويات وKubernetes» مجاني؟

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

ماذا ستتعلم في «الحاويات وKubernetes»؟

حوّل تطبيق NestJS إلى حاوية باستخدام Docker، وتعلّم استراتيجيات النشر الأساسية باستخدام Kubernetes للاستضافة القابلة للتوسع. تتمرن على NestJS Enterprise Backend APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ NestJS Enterprise Backend APIs؟

لا تُشترط خبرة سابقة. NestJS Enterprise Backend APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 5 من أصل 6.

كم من الوقت يستغرق درس «الحاويات وKubernetes»؟

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

هل يمكنني كتابة وتشغيل أكواد في درس NestJS Enterprise Backend APIs هذا؟

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

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

  1. اختبارات الوحدة والاختبارات الشاملة
  2. أطرا Hardhat وTruffle
  3. اختبار أداء واجهات API
  4. اختبار الوحدات للعقود الذكية
  5. الحاويات وKubernetes
  6. النشر على شبكات الاختبار والشبكة الرئيسية
← العودة إلى NestJS Enterprise Backend APIs