N-Docs LogoN-Docs

Docker

Complete beginner-friendly guide to Docker installation, commands, and containerization

Introduction

Docker is a platform for containerization that packages applications and their dependencies into portable, lightweight containers. It revolutionizes how we deploy and manage applications across different environments.

Why Choose Docker?

Docker solves the "it works on my machine" problem by ensuring consistent environments from development to production.

Key Benefits

Consistency

Works identically across development, testing, and production environments.

Isolation

Each application runs in its own container without affecting others.

Lightweight

Containers share the OS kernel, making them faster and smaller than VMs.

Scalability

Easy horizontal scaling and microservices architecture support.

Installation Guide

Essential Commands

Docker Compose

Docker Compose simplifies multi-container application deployment using declarative YAML configuration.

Modern Compose

Use docker compose (with space) instead of docker-compose for the latest version integrated with Docker CLI.

Basic Configuration

docker-compose.yml
services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html
    depends_on:
      - database
    environment:
      - NODE_ENV=production
  
  database:
    image: postgres:15-alpine
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    volumes:
      - db_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"

volumes:
  db_data:
    driver: local

networks:
  default:
    driver: bridge

Essential Commands

Advanced Patterns

Quick Start Example

Let's deploy a simple web application to see Docker in action:

# 1. Pull the nginx image
docker pull nginx:alpine

# 2. Run nginx container with port mapping
docker run -d -p 8080:80 --name my-nginx nginx:alpine

# 3. Check if it's running
docker ps

# 4. Visit http://localhost:8080 in your browser

# 5. View logs
docker logs my-nginx

# 6. Stop and remove
docker stop my-nginx
docker rm my-nginx

Success!

If you see the nginx welcome page at http://localhost:8080, you've successfully run your first Docker container!

Best Practices

Common Patterns

LAMP Stack

Linux, Apache, MySQL, PHP application stack

services:
  web:
    image: php:8.1-apache
    ports:
      - "80:80"
    volumes:
      - ./src:/var/www/html
    depends_on:
      - database
  
  database:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: rootpass
      MYSQL_DATABASE: myapp

Node.js + Redis

Node.js application with Redis cache

services:
  app:
    build: .
    ports:
      - "3000:3000"
    environment:
      - REDIS_URL=redis://redis:6379
    depends_on:
      - redis
  
  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"

Troubleshooting

Learning Path

Docker Mastery Roadmap

Follow this structured path to become proficient with Docker containerization.

Phase 1: Foundations (Week 1-2)

  • Installation & Setup - Get Docker running on your system
  • Basic Commands - Learn essential Docker CLI commands
  • Images & Containers - Understand the core concepts
  • 🎯 Practice: Deploy nginx, postgres, and redis containers

Phase 2: Development (Week 3-4)

  • 📝 Dockerfiles - Create custom images for your applications
  • 🔧 Docker Compose - Multi-container application orchestration
  • 💾 Volumes & Networks - Data persistence and container communication
  • 🎯 Practice: Containerize a full-stack application

Phase 3: Production (Week 5-6)

  • 🔒 Security - Container security best practices
  • Optimization - Image size and performance optimization
  • 📊 Monitoring - Logging and health checks
  • 🎯 Practice: Deploy production-ready applications

Phase 4: Advanced (Week 7-8)

  • 🏗️ Multi-stage Builds - Optimize build processes
  • 🌐 Registry Management - Private registries and image distribution
  • 🔄 CI/CD Integration - Automated building and deployment
  • 🎯 Practice: Set up automated deployment pipelines

Next Steps

Ready to advance your containerization skills?