Complete Docker Deep Knowledge Guide
1. What is Docker?
Docker is a platform for containerizing applications.
A container is:
- A lightweight, isolated environment for running an application.
- It packages everything needed: source code, libraries, dependencies, and runtime.
Comparison:
VM (Virtual Machine) vs Docker Container
- Heavy (GBs) vs Lightweight (MBs)
- Full OS per app vs Shared OS kernel
- Slow boot (minutes) vs Fast boot (seconds)
- Resource intensive vs Resource efficient
2. Docker Architecture
Main components:
- Docker Engine: Client-server app on your machine.
- Docker Daemon (dockerd): Runs in background, manages containers/images.
- Docker CLI (docker): Command-line interface to interact with Docker.
- Docker Hub: Online repo to share/download images (like GitHub but for images).
3. Images vs Containers
Image vs Container
- Image: Blueprint (immutable)
- Container: Running instance (mutable)
Dockerfile Example:
FROM node:20
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
CMD ["node", "index.js"]
4. Key Docker Commands
- docker pull node:20
- docker build -t myapp:latest .
- docker run -d -p 3000:3000 myapp:latest
- docker ps
- docker ps -a
- docker stop <id>
- docker rm <id>
- docker rmi myapp:latest
5. Data Persistence: Volumes
Containers lose data when stopped - unless you mount a volume.
- Volumes can be named or bind-mounted.
Command:
docker run -v /my/host/folder:/app/data myapp
6. Multi-Stage Builds
Optimizes image size.
Example:
FROM node:20 AS builder
WORKDIR /app
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
7. Docker Compose
Used to define & run multi-container apps.
docker-compose.yml:
version: '3.8'
services:
app:
build: .
ports:
- "3000:3000"
db:
image: postgres:14
environment:
POSTGRES_PASSWORD: example
Run: docker-compose up -d
8. Advanced Docker Concepts
Networking:
- docker network create mynet
- docker run --network=mynet myapp
Health checks:
HEALTHCHECK CMD curl --fail http://localhost:3000 || exit 1
Logging & Monitoring:
- Integrated with Prometheus, Grafana, ELK
Security:
- Minimal base images (alpine)
- Avoid running apps as root
- Update base images
9. Use Cases of Docker
- Microservices
- Dev environments
- CI/CD pipelines
- Platform-independent apps
- Scaling applications (Kubernetes)
10. Summary: Why Use Docker
Benefit vs Reason:
- Consistency: Same app across dev/test/prod
- Portability: Run anywhere
- Efficiency: Lightweight, fast booting
- Isolation: Apps run independently
- Scalability: Easily replicate or scale
What to Learn Next?
- Docker Compose
- Multi-stage builds
- Docker Swarm / Kubernetes
- Container security best practices
- CI/CD with Docker