Question - Why we need docker?
-
Imagine two developers:
- Dev A builds an app on his laptop.
- Dev B tries to run it on her laptop.
-
Dev A says:
- It works perfectly on my machine!
Dev B runs it and gets 100 errors.
Why?
-
Because Dev A's computer has:
- Node v18
- Python 3.11
- Some libraries
- Special OS settings
Dev B's computer has different versions.
This problem existed for years.
Then came the hero ==> Docker
Question - What is Docker?
Docker is a containerization platform used to package an application and its dependencies into a lightweight container that runs consistently across environments.
-
Key benefits:
- Environment consistency
- Faster deployment
- Isolation
- Lightweight compared to VMs
Question - What is the difference between Docker and Virtual Machines?
- Both provide isolated environments, but they work differently.
Feature Docker Containers Virtual Machines
---------------- ------------------- ------------------------
OS Share host OS Each VM has its own OS
Size MBs GBs
Startup Time Seconds Minutes
Performance Faster Slower
Resource Usage Lightweight Heavy
- Containers are more efficient because they share the host kernel.
Question - Main components of Docker:
- Docker Client
- Docker Daemon
- Docker Images
- Docker Containers
- Dockerfile
- Docker Registry (Docker Hub)
Docker Client
- The Docker Client is the tool developers use to interact with Docker.
- When you run commands like:
docker build
docker run
docker pull
- You are using the Docker Client.
- The client sends these commands to the Docker Daemon.
Docker Daemon
- The Docker Daemon (dockerd) is the background service that manages everything.
- It is responsible for:
- Building images
- Running containers
- Managing networks
- Managing volumes
- The Docker client communicates with the daemon to perform actions.
Docker Image
- A Docker Image is a read-only template used to create containers.
- It contains:
- Application code
- Runtime (Node, Python, etc.)
- Libraries
- Dependencies
- Environment setup
- Example image:
docker pull nginx
Docker Container
- A Docker Container is a running instance of an image.
- Think of it like:
Image = Blueprint
Container = Running Application
- Containers are:
- Lightweight
- Fast
- Isolated
Dockerfile
- A Dockerfile is a script that defines how to build a Docker image.
- Example:
FROM node:18
WORKDIR /app
COPY . .
RUN npm install
CMD ["npm","start"]
- This tells Docker how to build the image step by step.
Docker Registry
- A Docker Registry is a place where Docker images are stored and shared.
- The most popular public registry is: Dcoker Hub
- Example Command:
docker pull ubuntu
- This downloads the image from Docker Hub.
Question - Most Important Dockerfile Commands
FROM Base image
WORKDIR Set working directory
COPY Copy files
ADD Copy + extract
RUN Execute commands
CMD Default command
ENTRYPOINT Main executable
EXPOSE Declare port
ENV Environment variable
Question - Difference Between CMD and ENTRYPOINT
CMD ENTRYPOINT
Default command Fixed executable
Can be overridden Harder to override
Used for parameters Used for main command
- Example:
CMD ["node", "server.js"]
Question - What is .dockerignore?
- Similar to .gitignore, it prevents unnecessary files from being copied to the Docker image.
- Example:
node_modules
.git
.env
Question - How to Build a Docker Image?
docker build -t my-node-app .
Question - How to Run a Container?
docker run -p 3000:3000 my-node-app
Question - How to See Running Containers?
docker ps
- All containers:
docker ps -a
Question - How to Run a Container?
docker stop container_id
Question - What is Docker Compose?
- Docker Compose is a tool used to define and run multi-container Docker applications using a docker-compose.yml file.
- Example use:
- Node.js
- MongoDB
- Redis
- All run together
Question - Example docker-compose.yml for Node.js + Mongo
version: "3"
services:
app:
build: .
ports:
- "3000:3000"
depends_on:
- mongo
mongo:
image: mongo
ports:
- "27017:27017"
Question - Important Docker Compose Commands
- Start services
docker-compose up
- Background mode
docker-compose up -d
- Stop services
docker-compose down
- Rebuild
docker-compose up --build
Question - What is depends_on in Docker Compose?
- It defines service dependency order.
- Example :
depends_on:
- mongo
- Meaning Node starts after Mongo container.
Question - What is build vs image?
- build
- Builds image from Dockerfile
- Used for custom apps
- Example :
build: .
- image
- Uses existing image
- Used for prebuilt apps
- Example :
image: mongo
Question - What is a Docker Volume?
- A Docker Volume is used for persistent data storage outside the container.
- Normally when a container stops or is deleted, data is lost.
- Volumes solve this problem.
- Example :
docker run -v myvolume:/data nginx
- Common use cases :
- Databases
- Logs
- File uploads
Question - What is Bind Mount in Docker?
- A Bind Mount connects a host machine directory to a container directory.
- Example :
docker run -v /host/folder:/container/folder nginx
- Difference from Volume:
Volume Bind Mount
----------------------- -----------------
Managed by Docker Managed by host
Stored in Docker area Any host path
Portable Less portable
Question - What is Docker Network?
- Docker Network allows containers to communicate with each other.
- Types of networks:
Network Type Description
-------------- --------------------------------
bridge Default network for containers
host Container shares host network
none No networking
overlay Used in Docker Swarm
- Example:
docker network create mynetwork
Question - What is Docker Layer Caching?
- Docker images are built in layers.
- Each command in Dockerfile creates a new layer.
- Example:
FROM node:18
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
- If package.json doesn’t change, Docker reuses the cached layer.
- Benefits:
- Faster builds
- Reduced rebuild time
Top comments (0)