Back to Cloud & DevOps

Module 3: Docker & Containerization

Package your applications with all dependencies into portable containers

📦 What is Docker?

Imagine shipping containers - they're standardized boxes that can hold anything and work on any ship, truck, or train. Docker containers work the same way for software! Package your app with everything it needs (code, libraries, dependencies) into a container that runs identically everywhere - your laptop, test server, or production cloud.

The Problem Docker Solves

"It works on my machine!" - Every developer's nightmare. Your app works locally but breaks in production because of different OS versions, missing libraries, or configuration differences. Docker eliminates this by packaging everything together!

Why Use Docker?

Consistency

Same environment everywhere - dev, test, production

Isolation

Each container is isolated - no conflicts

Portability

Run anywhere - laptop, cloud, on-premises

Efficiency

Lightweight, starts in seconds, uses less resources

🔧 Docker Fundamentals

Core Concepts

Image

Blueprint for containers. Like a recipe - contains instructions to create a container. Immutable and can be shared via Docker Hub.

Container

Running instance of an image. Like a cake made from a recipe. Lightweight, isolated, and disposable.

Dockerfile

Text file with instructions to build an image. Defines base image, copies files, installs dependencies, sets commands.

Docker Hub

Public registry for Docker images. Like GitHub for containers. Find official images for Node.js, Python, databases, etc.

Essential Docker Commands

# Pull an image from Docker Hub

docker pull nginx

# Run a container

docker run -d -p 80:80 nginx

# List running containers

docker ps

# Stop a container

docker stop container_id

# Build image from Dockerfile

docker build -t myapp:1.0 .

# View logs

docker logs container_id

📝 Dockerfile Best Practices

A Dockerfile is your container's recipe. Write it well, and you'll have efficient, secure containers!

# Example Dockerfile for Node.js app

FROM node:18-alpine

# Set working directory

WORKDIR /app

# Copy package files

COPY package*.json ./

# Install dependencies

RUN npm install

# Copy application code

COPY . .

# Expose port

EXPOSE 3000

# Start application

CMD ["npm", "start"]

💡 Best Practices:

  • • Use official base images (node:alpine, python:slim)
  • • Order instructions from least to most frequently changing
  • • Use .dockerignore to exclude unnecessary files
  • • Minimize layers by combining RUN commands
  • • Don't run as root user
  • • Use multi-stage builds for smaller images

🎼 Docker Compose

Docker Compose lets you define and run multi-container applications. Instead of running multiple docker commands, define everything in a YAML file and start it all with one command!

# docker-compose.yml

version: '3.8'

services:

web:

build: .

ports:

- "3000:3000"

environment:

- DATABASE_URL=postgres://db:5432

depends_on:

- db

db:

image: postgres:15

environment:

- POSTGRES_PASSWORD=secret

volumes:

- db-data:/var/lib/postgresql/data

volumes:

db-data:

# Start all services

docker-compose up -d

# Stop all services

docker-compose down

# View logs

docker-compose logs -f

📚 Learning Resources

🎯 What's Next?

Awesome! You can now containerize applications with Docker. Next, we'll learn Kubernetes - the industry standard for orchestrating containers at scale. Get ready to manage hundreds of containers!