All posts

Docker Fundamentals: Complete Guide

· Pavan Y N · docker, containers, virtualization, fundamentals


# title: "Docker Fundamentals: Complete Guide to Containerization" date: "2025-10-19" tags: ["docker", "containers", "virtualization", "fundamentals"]

Docker Fundamentals: Complete Guide to Containerization

Docker has revolutionized how we build, ship, and run applications. This comprehensive guide covers everything you need to know about Docker fundamentals, from basic concepts to practical implementation.

# What is Docker?

Docker is a platform for developing, shipping, and running applications in containers. Containers package an application with all its dependencies, libraries, and configuration files, ensuring it runs consistently across different environments.

# Containers vs Virtual Machines

Understanding the difference is crucial:

## Virtual Machines (VMs)

  • →Include full operating system
  • →Larger size (GBs)
  • →Slower startup (minutes)
  • →Higher resource overhead
  • →Strong isolation

## Containers

  • →Share host OS kernel
  • →Smaller size (MBs)
  • →Fast startup (seconds)
  • →Lower resource overhead
  • →Process-level isolation

When to use what?

  • →Containers: Microservices, CI/CD, cloud-native apps
  • →VMs: Legacy applications, strong isolation needs, different OS requirements

# Docker Architecture

Docker uses a client-server architecture:

code
┌─────────────┐
│ Docker CLI  │ (Client)
└──────┬──────┘
       │
       ↓
┌─────────────┐
│Docker Daemon│ (Server)
└──────┬──────┘
       │
       ├→ Images
       ├→ Containers
       ├→ Networks
       └→ Volumes

## Key Components

  1. •Docker Daemon: Background service managing Docker objects
  2. •Docker Client: Command-line tool for interacting with daemon
  3. •Docker Registry: Stores Docker images (Docker Hub, private registries)
  4. •Docker Images: Read-only templates for creating containers
  5. •Docker Containers: Running instances of images

# Docker Images

Images are the building blocks of containers.

## Image Layers

Docker images are built in layers:

code
FROM ubuntu:20.04          # Base layer
RUN apt-get update         # Layer 2
RUN apt-get install -y nginx  # Layer 3
COPY app /var/www/html     # Layer 4
CMD ["nginx", "-g", "daemon off;"]  # Layer 5

Each instruction creates a new layer. Layers are cached and reused, making builds faster.

## Working with Images

code
# Pull an image from Docker Hub
docker pull nginx:latest

# List local images
docker images

# Build an image from Dockerfile
docker build -t myapp:1.0 .

# Tag an image
docker tag myapp:1.0 myregistry/myapp:1.0

# Push to registry
docker push myregistry/myapp:1.0

# Remove an image
docker rmi myapp:1.0

# Dockerfile Best Practices

A Dockerfile defines how to build your image:

code
# Use specific version tags
FROM node:18-alpine

# Set working directory
WORKDIR /app

# Copy package files first (better caching)
COPY package*.json ./

# Install dependencies
RUN npm ci --only=production

# Copy application code
COPY . .

# Expose port
EXPOSE 3000

# Use non-root user
USER node

# Define startup command
CMD ["node", "server.js"]

## Best Practices

  1. •Use Official Base Images: Start with trusted, maintained images
  2. •Minimize Layers: Combine RUN commands where possible
  3. •Order Matters: Put frequently changing instructions last
  4. •Use .dockerignore: Exclude unnecessary files
  5. •Multi-stage Builds: Reduce final image size
  6. •Security: Run as non-root user, scan for vulnerabilities

# Docker Containers

Containers are running instances of images.

## Container Lifecycle

code
# Create and start a container
docker run -d --name myapp -p 8080:80 nginx

# List running containers
docker ps

# List all containers (including stopped)
docker ps -a

# Stop a container
docker stop myapp

# Start a stopped container
docker start myapp

# Restart a container
docker restart myapp

# Remove a container
docker rm myapp

# Remove all stopped containers
docker container prune

## Container Options

code
# Run with environment variables
docker run -e DB_HOST=localhost -e DB_PORT=5432 myapp

# Mount volumes
docker run -v /host/path:/container/path myapp

# Set resource limits
docker run --memory="512m" --cpus="1.0" myapp

# Run interactively
docker run -it ubuntu bash

# Run in background (detached)
docker run -d nginx

# Auto-remove after exit
docker run --rm myapp

# Docker Volumes

Volumes persist data beyond container lifecycle:

code
# Create a volume
docker volume create mydata

# Use volume in container
docker run -v mydata:/app/data myapp

# List volumes
docker volume ls

# Inspect volume
docker volume inspect mydata

# Remove volume
docker volume rm mydata

## Volume Types

  1. •Named Volumes: Managed by Docker
  2. •Bind Mounts: Mount host directory
  3. •tmpfs Mounts: Temporary in-memory storage

# Docker Networking

Docker provides several networking options:

## Network Types

  1. •Bridge (default): Isolated network for containers
  2. •Host: Use host's network directly
  3. •None: No networking
  4. •Overlay: Multi-host networking for Swarm
code
# Create a network
docker network create mynetwork

# Run container on network
docker run --network mynetwork myapp

# Connect running container to network
docker network connect mynetwork myapp

# List networks
docker network ls

# Inspect network
docker network inspect mynetwork

## Container Communication

code
# Containers on same network can communicate by name
docker network create appnet
docker run -d --name database --network appnet postgres
docker run -d --name webapp --network appnet myapp

# webapp can connect to database using hostname "database"

# Docker Compose

Manage multi-container applications:

code
version: '3.8'

services:
  web:
    build: .
    ports:
      - "8080:80"
    environment:
      - DB_HOST=database
    depends_on:
      - database
    networks:
      - appnet

  database:
    image: postgres:14
    environment:
      - POSTGRES_PASSWORD=secret
    volumes:
      - dbdata:/var/lib/postgresql/data
    networks:
      - appnet

volumes:
  dbdata:

networks:
  appnet:
code
# Start all services
docker-compose up -d

# Stop all services
docker-compose down

# View logs
docker-compose logs -f

# Scale a service
docker-compose up -d --scale web=3

# Troubleshooting Common Issues

## Container Won't Start

code
# Check logs
docker logs myapp

# Check last 100 lines
docker logs --tail 100 myapp

# Follow logs in real-time
docker logs -f myapp

## Container Exits Immediately

code
# Check exit code
docker ps -a

# Inspect container
docker inspect myapp

# Run with interactive shell
docker run -it myapp /bin/bash

## Port Already in Use

code
# Find process using port
netstat -tulpn | grep :8080

# Use different host port
docker run -p 8081:80 nginx

## Out of Disk Space

code
# Remove unused images
docker image prune

# Remove unused containers
docker container prune

# Remove unused volumes
docker volume prune

# Remove everything unused
docker system prune -a

# Security Best Practices

  1. •Use Official Images: Start with trusted base images
  2. •Scan for Vulnerabilities: Use tools like Trivy or Snyk
  3. •Run as Non-Root: Create and use non-root users
  4. •Limit Resources: Set memory and CPU limits
  5. •Use Secrets: Don't hardcode sensitive data
  6. •Keep Updated: Regularly update base images
  7. •Minimal Images: Use alpine or distroless images

# Real-World Example

Complete Node.js application with Docker:

code
# Multi-stage build
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:18-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY package*.json ./
EXPOSE 3000
USER node
CMD ["node", "dist/server.js"]

# Conclusion

Docker fundamentals provide the foundation for modern application deployment. By understanding images, containers, volumes, and networking, you can build, ship, and run applications consistently across any environment.

# Next Steps

  1. •Install Docker on your machine
  2. •Run your first container: docker run hello-world
  3. •Build a simple Dockerfile for your application
  4. •Experiment with Docker Compose for multi-container apps
  5. •Learn about Docker security and best practices

Happy containerizing! 🐳