📖 WHAT IS DOCKER? CHAPTER 1

Docker is a platform for developing, shipping, and running applications using containerization. Containers package an application with all its dependencies, ensuring it runs consistently across different environments.

✓ Key Benefits

  • Consistent environments
  • Isolation and security
  • Resource efficiency
  • Easy scaling
  • Fast deployment

● Core Concepts

  • Image: Read-only template
  • Container: Running instance
  • Volume: Persistent storage
  • Network: Communication layer
  • Dockerfile: Build instructions
🏗 DOCKER ARCHITECTURE CHAPTER 2
┌─────────────────────────────┐
│         Docker Architecture              │
├─────────────────────────────┤
│  ┌──────────────┐    ┌──────────────┐  │
│  │   Client     │►►►│   Daemon     │  │
│  │   (CLI)       │    │  (dockerd)   │  │
│  └──────────────┘    └──────────────┘  │
│                              │            │
│                              ▼            │
│                     ┌──────────────┐   │
│                     │  Containers  │   │
│                     │   Images     │   │
│                     │   Volumes    │   │
│                     │   Networks   │   │
│                     └──────────────┘   │
│        └───────────► Registry          │
│                   (Docker Hub)         │
└─────────────────────────────┘

Docker Client: CLI tool to interact with Docker
Docker Daemon: Background service managing containers
Docker Registry: Repository for storing images (Docker Hub, private registries)

🖼 DOCKER IMAGES CHAPTER 3

A Docker image is a read-only template used to create containers. Images are built from Dockerfiles and consist of multiple layers stacked on top of each other.

L Image Layers
┌─────────────────────────┐
│   Application Layer     │  ← Your app code
├─────────────────────────┤
│   Dependencies Layer    │  ← npm/pip packages
├─────────────────────────┤
│   Runtime Layer         │  ← Node.js, Python...
├─────────────────────────┤
│   OS Layer              │  ← Alpine, Ubuntu...
└─────────────────────────┘
5 Image Lifecycle
1. Build — Create from Dockerfile
docker build -t myapp:latest .
2. Tag — Add name and version
docker tag myapp:latest myapp:v1.0.0
3. Push — Upload to registry
docker push myapp:latest
4. Pull — Download from registry
docker pull nginx:latest
5. Remove — Delete unused images
docker rmi myapp:latest
Common Commands
ActionCommand
List all imagesdocker images
Inspect image detailsdocker inspect image:tag
View image historydocker history image:tag
Save image to tardocker save -o image.tar image:tag
Load image from tardocker load -i image.tar
📦 DOCKER CONTAINERS CHAPTER 4

A container is a running instance of an image. Containers are isolated, lightweight, and share the host OS kernel.

L Container Lifecycle
Created → Running → Paused → Stopped → Removed
   │         │         │         │         │
   │         │         │         │         └─ docker rm
   │         │         │         └─ docker stop
   │         │         └─ docker pause/unpause
   │         └─ docker start
   └─ docker create

Container States

  • Created: Initialized but not started
  • Running: Currently executing
  • Paused: Temporarily suspended
  • Stopped: Exited or stopped
  • Removed: Deleted from system

Container Properties

  • Unique ID and name
  • Isolated filesystem
  • Network interface
  • Resource limits (CPU, mem)
  • Environment variables
Container Commands
Create & Run Detached
-d: detached mode, --name: container name
docker run -d --name mycontainer nginx:latest
Interactive Container
-it: interactive terminal
docker run -it ubuntu:latest /bin/bash
With Port Mapping
-p host:container maps port 8080 to container's port 80
docker run -d -p 8080:80 nginx:latest
With Environment Variables
-e: set environment variable
docker run -d -e DB_HOST=localhost -e DB_PORT=5432 myapp
ActionCommand
List running containersdocker ps
List all containersdocker ps -a
Stop containerdocker stop container_name
Start containerdocker start container_name
Remove containerdocker rm container_name
View logsdocker logs container_name
Execute commanddocker exec -it container_name /bin/bash
📜 DOCKERFILE CHAPTER 5

A Dockerfile is a text file containing instructions to build a Docker image. Each instruction creates a new layer.

I Common Instructions
FROM — Sets the base image
FROM node:18-alpine
WORKDIR — Sets working directory
WORKDIR /app
COPY / ADD — Copies files into image
COPY package.json . COPY . .
RUN — Executes commands during build
RUN npm install RUN apt-get update && apt-get install -y curl
ENV — Sets environment variables
ENV NODE_ENV=production ENV PORT=3000
EXPOSE — Documents which port app listens on
EXPOSE 3000
CMD / ENTRYPOINT — Default command to run
CMD ["npm", "start"] ENTRYPOINT ["node", "server.js"]
Example Dockerfile
# Node.js Application Dockerfile
FROM node:18-alpine

# Set working directory
WORKDIR /app

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm ci --only=production

# Copy application code
COPY . .

# Set environment variables
ENV NODE_ENV=production
ENV PORT=3000

# Expose port
EXPOSE 3000

# Health check
HEALTHCHECK --interval=30s --timeout=3s \
  CMD node healthcheck.js

# Run application
CMD ["node", "server.js"]
Multi-Stage Build
# Build stage
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

# Production stage
FROM node:18-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY package*.json ./
EXPOSE 3000
CMD ["node", "dist/server.js"]
💾 DOCKER VOLUMES CHAPTER 6

Volumes provide persistent storage for containers. Data in volumes persists even after containers are removed.

3 Volume Types

Named Volumes

Managed by Docker, stored in Docker's directory

docker volume create myvolume

Bind Mounts

Mount host directory into container

-v /host/path:/container/path

tmpfs Mounts

Stored in host memory (temporary)

--tmpfs /tmp
Volume Commands
Create Named Volume
docker volume create mydata
Use Volume in Container
docker run -d -v mydata:/data nginx:latest
Bind Mount
docker run -d -v /host/data:/container/data nginx:latest
ActionCommand
List volumesdocker volume ls
Inspect volumedocker volume inspect mydata
Remove volumedocker volume rm mydata
Remove unused volumesdocker volume prune
💡 Use Cases
  • Database data: Persist database files across container restarts
  • Configuration files: Share configs between host and container
  • Application logs: Store logs outside container
  • Source code: Mount code for development (bind mount)
  • Shared data: Share data between multiple containers
🌐 DOCKER NETWORKS CHAPTER 7

Docker networks enable communication between containers and with external networks. Containers can be isolated or connected.

4 Network Types
Bridge Network (Default)
Isolated network for containers on same host
docker network create mynetwork docker run --network=mynetwork nginx
Host Network
Container uses host's network directly
docker run --network=host nginx
None Network
No network access (complete isolation)
docker run --network=none nginx
Overlay Network
Multi-host networking for Swarm clusters
docker network create --driver overlay myoverlay
ActionCommand
List networksdocker network ls
Create networkdocker network create mynetwork
Inspect networkdocker network inspect mynetwork
Connect containerdocker network connect mynetwork container
Disconnect containerdocker network disconnect mynetwork container
Remove networkdocker network rm mynetwork
💬 Container Communication
# Create network
docker network create app-network

# Run containers on same network
docker run -d --name web --network app-network nginx
docker run -d --name db --network app-network postgres

# Containers can communicate using container names
# From web container: curl http://db:5432
🎵 DOCKER COMPOSE CHAPTER 8

Docker Compose is a tool for defining and running multi-container Docker applications using a YAML file.

Y docker-compose.yml Structure
version: '3.8'

services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
    volumes:
      - ./app:/app
    depends_on:
      - db
    networks:
      - app-network

  db:
    image: postgres:14
    environment:
      - POSTGRES_DB=mydb
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=pass
    volumes:
      - db-data:/var/lib/postgresql/data
    networks:
      - app-network

volumes:
  db-data:

networks:
  app-network:
    driver: bridge
Compose Commands
Start Services (detached)
docker-compose up -d
Stop Services
docker-compose down
View Logs (follow)
docker-compose logs -f
Scale Services
docker-compose up -d --scale web=3
Build Images
docker-compose build
★ BEST PRACTICES CHAPTER 9
I Image Best Practices
✓ Use Multi-stage Builds
Reduce final image size by using separate build and runtime stages
✓ Use .dockerignore
Exclude unnecessary files from build context
node_modules .git *.log
✓ Use Specific Tags
Avoid using 'latest' tag in production
FROM node:18.17.0-alpine
✓ Minimize Layers
Combine RUN commands to reduce layers
RUN apt-get update && apt-get install -y \ curl && rm -rf /var/lib/apt/lists/*
🔒 Security Best Practices
✓ Run as Non-root User
RUN useradd -m appuser USER appuser
✓ Scan Images for Vulnerabilities
docker scan myimage:tag
✓ Use Secrets Management
Never hardcode secrets in Dockerfiles — use environment variables or secret managers
✓ Limit Container Resources
docker run --memory="512m" --cpus="1.0" myapp
Performance Best Practices
✓ Leverage Build Cache
Order Dockerfile instructions from least to most frequently changing
✓ Use Alpine Images
Smaller base images reduce image size and attack surface
✓ Clean Up Regularly
docker system prune -a