Node.js Course
Node.js
/
Advanced

Docker Basics

Definition

A platform that uses OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files.

Explain Like I'm New

The ultimate solution to 'It works on my machine!'. Instead of just giving your code to another developer, you give them a magical, invisible box (Container). Inside the box is a mini Linux operating system, the exact version of Node.js you need, and your code. It doesn't matter if their computer is a Mac, Windows, or Linux; the box behaves exactly the same way everywhere.

Real World Example

Shipping code to AWS or Google Cloud. You write a `Dockerfile` that packages your Node app. AWS spins up the container in the cloud, guaranteeing 100% environment parity with your local laptop.

Common Use Cases

  • •Cloud-native deployments
  • •Microservices
  • •Consistent dev environments

Terminal Output

bash / terminal
# --- EXAMPLE DOCKERFILE FOR NODE.JS --- # 1. Start from a lightweight Linux OS with Node 18 pre-installed # FROM node:18-alpine # 2. Create an app directory inside the container # WORKDIR /usr/src/app # 3. Copy package.json first to utilize Docker layer caching # COPY package*.json ./ # 4. Install dependencies # RUN npm install --production # 5. Copy the rest of the application code # COPY . . # 6. Expose the port the app runs on # EXPOSE 3000 # 7. Start the app # CMD ["node", "server.js"] console.log("Docker ensures absolute consistency between Development, Staging, and Production.");

Interview Questions

basic

  • What is a Dockerfile?

intermediate

  • What is the difference between an Image and a Container?

Flash Cards

Question

What is a Dockerfile?

Click to reveal answer
Answer

A text file containing the exact instructions (recipe) on how to build the Docker Image (e.g., 'Download Node 18, copy my code, run npm install').

Question

Image vs Container?

Click to reveal answer
Answer

An Image is the static blueprint/CD-ROM. A Container is the actual running process spawned from that blueprint. You can spawn 50 identical containers from 1 image.