Introduction to Docker and Containerization
Docker revolutionized software deployment by packaging applications with all their dependencies into containers. Containers are lightweight, portable, and consistent across different environments, solving the "it works on my machine" problem.
What is Docker?
Docker is a platform for developing, shipping, and running applications in containers:
- Lightweight virtualization
- Consistent environments
- Fast deployment
- Resource efficient
- Portable across platforms
Containers vs Virtual Machines
Containers
- Share host OS kernel
- Lightweight (MBs)
- Start in seconds
- More efficient resource usage
Virtual Machines
- Full OS for each VM
- Heavy (GBs)
- Start in minutes
- More isolation
Docker Core Concepts
Images
Read-only templates for creating containers:
- Built from Dockerfile
- Layered file system
- Stored in registries
- Versioned with tags
Containers
Running instances of images:
- Isolated processes
- Writable layer on top of image
- Can be started, stopped, deleted
Dockerfile
Script to build Docker images:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
Essential Docker Commands
# Pull image
docker pull nginx:latest
# Run container
docker run -d -p 80:80 --name web nginx
# List containers
docker ps
docker ps -a # Include stopped
# Stop container
docker stop web
# Remove container
docker rm web
# View logs
docker logs web
# Execute command in container
docker exec -it web bash
# Build image
docker build -t myapp:1.0 .
# Push to registry
docker push username/myapp:1.0
Docker Compose
Define and run multi-container applications:
version: '3.8'
services:
web:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=production
depends_on:
- db
db:
image: postgres:14
environment:
- POSTGRES_PASSWORD=secret
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:
Compose Commands
# Start services
docker-compose up -d
# Stop services
docker-compose down
# View logs
docker-compose logs -f
# Scale services
docker-compose up -d --scale web=3
Docker Networking
- Bridge: Default network for containers
- Host: Use host's network directly
- Overlay: Multi-host networking
- None: No networking
Docker Volumes
Persist data beyond container lifecycle:
# Create volume
docker volume create mydata
# Use volume
docker run -v mydata:/app/data myapp
# List volumes
docker volume ls
# Remove volume
docker volume rm mydata
Best Practices
- Use official base images
- Keep images small
- Use multi-stage builds
- Don't run as root
- Use .dockerignore
- One process per container
- Tag images properly
- Scan for vulnerabilities
Container Orchestration
Kubernetes
- Automate deployment and scaling
- Self-healing
- Load balancing
- Rolling updates
Docker Swarm
- Native Docker clustering
- Simpler than Kubernetes
- Good for smaller deployments
Conclusion
Docker and containerization have transformed modern software development and deployment. Understanding containers, images, and orchestration is essential for building scalable, portable applications. Start with the basics and gradually explore advanced features as your needs grow.