الحاويات باستخدام Docker
أنشئ Dockerfiles، وابنِ الصور، وشغّل تطبيق Node.js في حاويات للحصول على بيئات متسقة.
الحاويات باستخدام Docker درس مجاني في Node.js Backend Development Bootcamp على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Node.js Backend Development Bootcamp، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Node.js Backend Development Bootcamp 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Docker: Consistent Environments
Welcome to containerization with Docker! Docker helps package your application and its dependencies into a single unit called a container.
This ensures your app runs consistently across different environments, from your laptop to production servers.
Images vs. Containers
Think of a Docker Image as a blueprint or a template. It's a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries, and settings.
A Docker Container is a running instance of an image. You can have multiple containers running from the same image, each isolated from the others.
Why Docker for Node.js?
Docker offers significant advantages for Node.js applications:
- Consistency: No more 'it works on my machine' issues.
- Isolation: Your Node.js app runs in its own environment, preventing conflicts.
- Dependencies: Easily manage and package all your npm dependencies.
- Scalability: Quickly deploy and scale identical instances of your app.
The Dockerfile Explained
A Dockerfile is a simple text file that contains a set of instructions. These instructions tell Docker how to build your application's image.
It defines everything from the base operating system to copying files, installing dependencies, and specifying how your application should start.
Simple Node.js App
Let's create a minimal Node.js application that we'll containerize. This simple server will respond with 'Hello from Node.js in Docker!' on port 3000.
You can run this code to see it working locally before Dockerizing.
const http = require('http');
const hostname = '0.0.0.0';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello from Node.js in Docker!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});Dockerfile: Base Image & Workdir
Every Dockerfile starts with a FROM instruction, specifying the base image. For Node.js, we often use official Node.js images. WORKDIR sets the working directory inside the container.
FROM node:18-alpine
WORKDIR /appDockerfile: Dependencies & Files
Next, we copy package.json and package-lock.json (if present) to install dependencies. Then, we copy the rest of our application files. This order optimizes Docker's caching.
COPY package*.json ./: Copies package files.RUN npm install: Installs dependencies.COPY . .: Copies all other app files.
COPY package*.json ./
RUN npm install
COPY . .Dockerfile: Port & Start Command
Finally, we tell Docker which port our application listens on using EXPOSE. This is documentation, not a firewall rule. CMD defines the command to run when the container starts.
EXPOSE 3000
CMD ["npm", "start"]Building Your Docker Image
With your Dockerfile ready (and your app.js and package.json in the same directory), you can build the image. The -t flag tags your image with a name (e.g., my-node-app).
Open your terminal and run:
docker build -t my-node-app .Running Your Docker Container
After building, you can run your application in a Docker container. The -p 3000:3000 flag maps port 3000 on your host machine to port 3000 inside the container.
Visit http://localhost:3000 in your browser after running:
docker run -p 3000:3000 my-node-appDockerfile Instruction Check
Which of the following Dockerfile instructions are primarily used to manage dependencies and application files within the image?
Recap: Docker for Node.js
You've learned the basics of Docker containerization! We covered:
- What Docker Images and Containers are.
- The benefits of Docker for Node.js apps.
- How to write a Dockerfile for a Node.js application.
- Commands to build an image and run a container.
Docker provides a powerful way to ensure your applications are consistent and easily deployable.
تعلم JavaScript مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 22
- الدروس
- 92
الأسئلة الشائعة
هل درس «الحاويات باستخدام Docker» مجاني؟
نعم — نص درس «الحاويات باستخدام Docker» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Node.js Backend Development Bootcamp، انتقل إلى CoddyKit PRO. تتضمن دورة Node.js Backend Development Bootcamp 4 دروس في المجموع.
ماذا ستتعلم في «الحاويات باستخدام Docker»؟
أنشئ Dockerfiles، وابنِ الصور، وشغّل تطبيق Node.js في حاويات للحصول على بيئات متسقة. تتمرن على Node.js Backend Development Bootcamp مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Node.js Backend Development Bootcamp؟
لا تُشترط خبرة سابقة. Node.js Backend Development Bootcamp على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «الحاويات باستخدام Docker»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Node.js Backend Development Bootcamp هذا؟
نعم. كل درس في Node.js Backend Development Bootcamp يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- الحاويات باستخدام Docker
- النشر السحابي (AWS/Heroku)
- إدارة العمليات باستخدام PM2
- مسارات CI/CD باستخدام GitHub Actions