π³ What is Docker?
Docker is a platform that packages applications and their dependencies into lightweight, portable containers. Think of containers as standardized units that include everything needed to run your application: code, runtime, libraries, and system tools.
Why Docker in 2026?
- Consistency: "Works on my machine" is no longer an excuse
- Isolation: Each container runs independently
- Portability: Run anywhere - dev, staging, production
- Efficiency: Lightweight compared to VMs
- Scalability: Easy to scale up or down
- DevOps Standard: Industry-standard for deployment
π Getting Started with Docker
Installation
# Windows/Mac: Download Docker Desktop # https://www.docker.com/products/docker-desktop # Linux (Ubuntu/Debian): curl -fsSL https://get.docker.com -o get-docker.sh sudo sh get-docker.sh # Verify installation docker --version docker run hello-world
Key Concepts
- Image: Blueprint/template for containers (like a class in OOP)
- Container: Running instance of an image (like an object)
- Dockerfile: Instructions to build an image
- Registry: Storage for images (Docker Hub, GitHub Container Registry)
- Volume: Persistent data storage
- Network: Communication between containers
π¦ Essential Docker Commands
Working with Images
# Pull an image from Docker Hub docker pull nginx:latest docker pull node:20-alpine # List all images docker images # Remove an image docker rmi nginx:latest # Build image from Dockerfile docker build -t myapp:1.0 . # Tag an image docker tag myapp:1.0 username/myapp:1.0 # Push to registry docker push username/myapp:1.0
Working with Containers
# Run a container docker run -d -p 8080:80 --name mynginx nginx # List running containers docker ps # List all containers (including stopped) docker ps -a # Stop a container docker stop mynginx # Start a stopped container docker start mynginx # Restart a container docker restart mynginx # Remove a container docker rm mynginx # View container logs docker logs mynginx docker logs -f mynginx # Follow logs # Execute command in running container docker exec -it mynginx bash # View container stats docker stats mynginx
π Creating a Dockerfile
Node.js Application Example
# Use official Node.js image FROM node:20-alpine # Set working directory WORKDIR /app # Copy package files COPY package*.json ./ # Install dependencies RUN npm ci --only=production # Copy application code COPY . . # Expose port EXPOSE 3000 # Set environment to production ENV NODE_ENV=production # Create non-root user RUN addgroup -g 1001 -S nodejs RUN adduser -S nodejs -u 1001 USER nodejs # Start application CMD ["node", "server.js"]
Multi-Stage Build (Best Practice)
# Stage 1: Build FROM node:20-alpine AS builder WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build # Stage 2: Production FROM node:20-alpine WORKDIR /app COPY package*.json ./ RUN npm ci --only=production COPY --from=builder /app/dist ./dist EXPOSE 3000 CMD ["node", "dist/server.js"]
Dockerfile Best Practices
- Use official base images: More secure and maintained
- Use specific tags:
node:20-alpinenotnode:latest - Multi-stage builds: Smaller final images
- Layer caching: Put frequently changing files last
- .dockerignore: Exclude unnecessary files
- Non-root user: Better security
- Minimize layers: Combine RUN commands
- Clean up: Remove cache and temp files
.dockerignore Example
node_modules npm-debug.log .git .gitignore README.md .env .env.local dist coverage .vscode .idea *.md
π Docker Compose
Docker Compose lets you define and run multi-container applications. Perfect for development environments with databases, caching, and multiple services.
docker-compose.yml Example
version: '3.8'
services:
# Node.js Application
app:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=production
- DATABASE_URL=postgresql://postgres:password@db:5432/myapp
- REDIS_URL=redis://redis:6379
depends_on:
- db
- redis
volumes:
- ./logs:/app/logs
restart: unless-stopped
# PostgreSQL Database
db:
image: postgres:16-alpine
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
- POSTGRES_DB=myapp
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"
restart: unless-stopped
# Redis Cache
redis:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
- redis_data:/data
restart: unless-stopped
# Nginx Reverse Proxy
nginx:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
depends_on:
- app
restart: unless-stopped
volumes:
postgres_data:
redis_data:Docker Compose Commands
# Start all services docker-compose up -d # View logs docker-compose logs -f # Stop all services docker-compose down # Stop and remove volumes docker-compose down -v # Rebuild and start docker-compose up -d --build # Scale a service docker-compose up -d --scale app=3 # Execute command in service docker-compose exec app sh
πΎ Volumes & Data Persistence
Types of Volumes
- Named Volumes: Managed by Docker, best for production
docker run -v mydata:/app/data myapp - Bind Mounts: Map host directory, good for development
docker run -v /host/path:/container/path myapp - tmpfs Mounts: Temporary in-memory storage
docker run --tmpfs /app/temp myapp
Volume Commands
# Create volume docker volume create mydata # List volumes docker volume ls # Inspect volume docker volume inspect mydata # Remove volume docker volume rm mydata # Remove unused volumes docker volume prune
π Docker Networking
Network Types
- bridge: Default, containers on same host communicate
- host: Container uses host network directly
- none: No networking
- overlay: Multi-host networking (Docker Swarm)
- custom: User-defined networks
Network Commands
# Create network docker network create mynetwork # List networks docker network ls # Connect container to network docker network connect mynetwork mycontainer # Disconnect docker network disconnect mynetwork mycontainer # Inspect network docker network inspect mynetwork # Remove network docker network rm mynetwork
π Security Best Practices
Security Checklist
- β Use official images from trusted sources
- β Scan images for vulnerabilities (docker scan)
- β Run containers as non-root user
- β Use read-only file systems where possible
- β Limit container resources (CPU, memory)
- β Don't store secrets in images
- β Use Docker secrets or environment variables
- β Keep Docker and images updated
- β Use minimal base images (alpine)
- β Enable Docker Content Trust
π Deployment Strategies
1. Docker Swarm
Built-in orchestration for Docker. Good for small to medium deployments.
# Initialize swarm docker swarm init # Deploy stack docker stack deploy -c docker-compose.yml myapp # Scale service docker service scale myapp_web=5 # List services docker service ls
2. Kubernetes
Industry standard for large-scale container orchestration. More complex but powerful.
3. Cloud Platforms
- AWS ECS/EKS: Elastic Container Service / Kubernetes
- Google Cloud Run: Serverless containers
- Azure Container Instances: Simple container hosting
- DigitalOcean App Platform: Easy deployment
- Heroku: Container-based deployment
π οΈ Useful Docker Tools
Development
- β’ Docker Desktop: GUI for Docker
- β’ Portainer: Web-based management
- β’ Lazydocker: Terminal UI
- β’ Dive: Image layer explorer
Production
- β’ Watchtower: Auto-update containers
- β’ Traefik: Reverse proxy
- β’ Prometheus: Monitoring
- β’ Grafana: Visualization
π‘ Common Issues & Solutions
Issue: Container exits immediately
Solution:
- Check logs:
docker logs container_name - Ensure CMD/ENTRYPOINT runs a long-lived process
- Use
docker run -itfor interactive debugging
Issue: Port already in use
Solution:
- Change host port:
-p 8080:80instead of-p 80:80 - Stop conflicting service
- Find process:
lsof -i :80(Linux/Mac)
Issue: Image build is slow
Solution:
- Use .dockerignore to exclude files
- Leverage layer caching
- Use multi-stage builds
- Use BuildKit:
DOCKER_BUILDKIT=1 docker build
π― Conclusion
Docker has become essential for modern software development. It solves the "works on my machine" problem, simplifies deployment, and enables microservices architecture. Start with simple containers, learn Docker Compose for multi-container apps, and gradually explore orchestration tools like Kubernetes.
The key is to practice: containerize your projects, experiment with different configurations, and learn from real-world scenarios. Docker's learning curve is worth itβit's a skill that will serve you throughout your career.
π³ Generate Dockerfiles Instantly
Skip the manual work! Use our free Dockerfile Generator to create optimized Dockerfiles for Node.js, Python, PHP, and Java applications in seconds.
Try Dockerfile Generator β