Docker for Beginners: Complete Containerization Guide

Docker for Beginners: Complete Containerization Guide
Docker has revolutionized how we build, ship, and run applications. This comprehensive guide covers everything you need to know to get started with containerization.
What is Docker?
Docker is a platform that uses containerization technology to package applications and their dependencies into portable containers. These containers can run consistently across different environments.
Key Benefits
- Consistency: Same environment from development to production
- Isolation: Applications run independently
- Portability: Run anywhere Docker is installed
- Efficiency: Lightweight compared to virtual machines
- Scalability: Easy horizontal scaling
Core Concepts
Images
Docker images are read-only templates containing the application code, runtime, libraries, and dependencies.
Containers
Containers are running instances of images - isolated environments where your application executes.
Dockerfile
A text file with instructions to build a Docker image.
Docker Compose
A tool for defining and running multi-container applications.
Installation
macOS
# Install Docker Desktop from docker.com # Or use Homebrew brew install --cask docker
Linux (Ubuntu)
# Update packages sudo apt-get update # Install dependencies sudo apt-get install apt-transport-https ca-certificates curl software-properties-common # Add Docker's GPG key curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg # Add Docker repository echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null # Install Docker sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io
Basic Commands
# Pull an image docker pull nginx # List images docker images # Run a container docker run -d -p 80:80 --name my-nginx nginx # List running containers docker ps # List all containers (including stopped) docker ps -a # Stop a container docker stop my-nginx # Remove a container docker rm my-nginx # Remove an image docker rmi nginx
Creating Your First Dockerfile
Here's a simple Node.js application Dockerfile:
# Base image 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 command CMD ["npm", "start"]
Build and run:
docker build -t my-node-app . docker run -d -p 3000:3000 my-node-app
Docker Compose Example
Create docker-compose.yml:
version: '3.8' services: web: build: . ports: - "3000:3000" environment: - NODE_ENV=development volumes: - .:/app - /app/node_modules depends_on: - db db: image: postgres:15-alpine environment: POSTGRES_DB: myapp POSTGRES_USER: user POSTGRES_PASSWORD: password volumes: - postgres_data:/var/lib/postgresql/data ports: - "5432:5432" volumes: postgres_data:
Run with:
docker-compose up -d docker-compose down
Best Practices
- Use specific image tags instead of
latest - Multi-stage builds for smaller images
- Don't run as root - use non-root users
- Use .dockerignore to exclude unnecessary files
- Layer caching - order instructions for optimal caching
- Health checks for container monitoring
Multi-Stage Build Example
# Build stage FROM node:18-alpine AS builder WORKDIR /app COPY package*.json ./ RUN npm ci --only=production # Production stage FROM node:18-alpine WORKDIR /app COPY --from=builder /app/node_modules ./node_modules COPY . . USER node EXPOSE 3000 CMD ["node", "server.js"]
Conclusion
Docker simplifies application deployment and ensures consistency across environments. Start with basic containers, then explore Docker Compose for multi-container applications, and eventually container orchestration with Kubernetes for production workloads.

About Dimuthu Wayaman
Mobile Application Developer and UI Designer specializing in Flutter development. Passionate about creating beautiful, functional mobile applications and sharing knowledge with the developer community.