Chapter 1: Docker Introduction & Installation - Complete Containerization Guide
Authored by syscook.dev
What is Docker?
Docker is a powerful containerization platform that enables developers to package applications and their dependencies into lightweight, portable containers. This comprehensive guide will teach you everything you need to know about Docker and how to use it effectively in your development workflow.
Why Learn Docker?
Docker has revolutionized software development and deployment because it:
- Consistency: "It works on my machine" becomes "it works everywhere"
- Portability: Run applications on any system that supports Docker
- Efficiency: Lightweight containers use fewer resources than virtual machines
- Scalability: Easy to scale applications up or down
- DevOps Integration: Essential for modern CI/CD pipelines
- Industry Standard: Used by major companies and cloud platforms
Docker is a containerization platform that allows you to package applications and their dependencies into lightweight, portable containers. These containers can run consistently across different environments, from development to production, solving the "it works on my machine" problem.
Key Concepts:
- Container: A lightweight, standalone package that includes everything needed to run an application
- Image: A read-only template used to create containers
- Dockerfile: A text file with instructions to build Docker images
- Registry: A storage and distribution system for Docker images
- Docker Engine: The runtime that manages containers
- Docker Compose: A tool for defining and running multi-container applications
Why Use Docker?
1. Consistency Across Environments
Docker ensures your application runs the same way everywhere.
# Same container runs on different machines
docker run -p 8080:80 nginx
# Works identically on development, staging, and production
Benefits:
- Eliminates environment-specific bugs
- Reduces deployment issues
- Simplifies testing and debugging
2. Resource Efficiency
Containers share the host OS kernel, making them more efficient than virtual machines.
# Compare resource usage
docker stats
# Shows CPU, memory, and network usage for running containers
Resource Comparison:
- Virtual Machines: Each VM needs its own OS (GBs of memory)
- Docker Containers: Share host OS kernel (MBs of memory)
3. Rapid Deployment
Containers start in seconds, not minutes.
# Start a web server in seconds
docker run -d -p 80:80 --name web-server nginx
# Scale to multiple instances
docker run -d -p 8081:80 --name web-server-2 nginx
docker run -d -p 8082:80 --name web-server-3 nginx
Docker Architecture
1. Client-Server Architecture
graph TB
A[Docker Client] --> B[Docker Daemon]
B --> C[Images]
B --> D[Containers]
B --> E[Networks]
B --> F[Volumes]
C --> G[Docker Registry]
H[Dockerfile] --> C
Components:
- Docker Client: Command-line interface for users
- Docker Daemon: Background service managing containers
- Docker Registry: Repository for storing images
- Dockerfile: Instructions for building images
2. Container Lifecycle
stateDiagram-v2
[*] --> Created: docker create
Created --> Running: docker start
Running --> Paused: docker pause
Paused --> Running: docker unpause
Running --> Stopped: docker stop
Stopped --> Running: docker start
Stopped --> [*]: docker rm
Running --> [*]: docker kill
How to Install Docker?
1. Linux Installation (Ubuntu/Debian)
Prerequisites
# Update package index
sudo apt-get update
# Install required packages
sudo apt-get install -y \
ca-certificates \
curl \
gnupg \
lsb-release
Add Docker's Official GPG Key
# Create directory for Docker's GPG key
sudo mkdir -p /etc/apt/keyrings
# Add Docker's GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
Add Docker Repository
# Add Docker repository
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Install Docker Engine
# Update package index
sudo apt-get update
# Install Docker Engine
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# Verify installation
sudo docker --version
sudo docker run hello-world
Post-Installation Steps
# Add user to docker group (avoid using sudo)
sudo usermod -aG docker $USER
# Log out and log back in, or run:
newgrp docker
# Verify non-root access
docker run hello-world
2. macOS Installation
Docker Desktop for Mac
# Download Docker Desktop from:
# https://www.docker.com/products/docker-desktop/
# Or install via Homebrew
brew install --cask docker
# Start Docker Desktop
open /Applications/Docker.app
Verify Installation
# Check Docker version
docker --version
# Test Docker installation
docker run hello-world
# Check Docker Compose
docker compose version
3. Windows Installation
Docker Desktop for Windows
# Download Docker Desktop from:
# https://www.docker.com/products/docker-desktop/
# Or install via Chocolatey
choco install docker-desktop
# Or install via winget
winget install Docker.DockerDesktop
System Requirements
- Windows 10 64-bit: Pro, Enterprise, or Education (Build 15063 or later)
- Windows 11 64-bit: Home or Pro
- WSL 2 feature enabled
- Virtualization enabled in BIOS
Verify Installation
# Check Docker version
docker --version
# Test Docker installation
docker run hello-world
# Check Docker Compose
docker compose version
4. Docker Compose Installation
Standalone Installation (Linux)
# Download Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# Make it executable
sudo chmod +x /usr/local/bin/docker-compose
# Verify installation
docker-compose --version
Using Docker Compose Plugin (Recommended)
# Docker Compose is included with Docker Desktop
# For Linux, install the plugin:
sudo apt-get install docker-compose-plugin
# Use with 'docker compose' command
docker compose version
Docker Configuration
1. Docker Daemon Configuration
Linux Configuration File
// /etc/docker/daemon.json
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
},
"storage-driver": "overlay2",
"data-root": "/var/lib/docker",
"registry-mirrors": [
"https://mirror.gcr.io"
],
"insecure-registries": [
"localhost:5000"
],
"live-restore": true,
"userland-proxy": false,
"experimental": false
}
Apply Configuration
# Restart Docker daemon
sudo systemctl restart docker
# Check configuration
docker info
2. Docker Client Configuration
Docker Context Management
# List available contexts
docker context ls
# Create new context
docker context create my-context --docker host=tcp://remote-host:2376
# Use specific context
docker context use my-context
# Show current context
docker context show
3. Resource Limits Configuration
Docker Desktop Settings (macOS/Windows)
// Docker Desktop settings
{
"cpu": 4,
"memory": 8192,
"disk": 100,
"swap": 2048
}
Linux Resource Limits
# Set memory limit for Docker daemon
sudo systemctl edit docker
# Add the following:
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --memory=8g
# Restart Docker
sudo systemctl restart docker
Verification and Testing
1. Basic Docker Commands Test
# Check Docker version
docker --version
docker compose version
# Check Docker info
docker info
# List running containers
docker ps
# List all containers
docker ps -a
# List images
docker images
# Test with hello-world
docker run hello-world
2. Docker Compose Test
# docker-compose.test.yml
version: '3.8'
services:
web:
image: nginx:alpine
ports:
- "8080:80"
environment:
- NGINX_HOST=localhost
- NGINX_PORT=80
# Test Docker Compose
docker compose -f docker-compose.test.yml up -d
docker compose -f docker-compose.test.yml ps
docker compose -f docker-compose.test.yml down
3. Performance Test
# Run a simple performance test
docker run --rm -it --name stress-test \
--memory=512m --cpus=1 \
stress:latest --cpu 1 --timeout 30s
# Monitor resource usage
docker stats stress-test
Troubleshooting Common Issues
1. Permission Denied Errors
# Add user to docker group
sudo usermod -aG docker $USER
# Log out and log back in
# Or run: newgrp docker
# Check group membership
groups $USER
2. Docker Daemon Not Running
# Start Docker daemon (Linux)
sudo systemctl start docker
sudo systemctl enable docker
# Check Docker daemon status
sudo systemctl status docker
# Check Docker daemon logs
sudo journalctl -u docker.service
3. Port Already in Use
# Find process using port
sudo netstat -tulpn | grep :80
sudo lsof -i :80
# Kill process using port
sudo kill -9 <PID>
# Or use different port
docker run -p 8080:80 nginx
4. Out of Disk Space
# Clean up unused resources
docker system prune -a
# Remove unused volumes
docker volume prune
# Remove unused networks
docker network prune
# Check disk usage
docker system df
Next Steps
Now that you have Docker installed and configured, you're ready to:
- Learn about Docker Images and Containers - Understanding the building blocks
- Master Dockerfile Creation - Building custom images
- Explore Docker Compose - Managing multi-container applications
- Understand Docker Networking - Connecting containers
- Learn Docker Storage - Managing data persistence
- Deploy to Production - Best practices for production environments
Summary
Docker is a powerful containerization platform that solves many deployment and environment consistency problems. With proper installation and configuration, you can:
- Package applications into portable containers
- Ensure consistency across different environments
- Improve resource efficiency compared to virtual machines
- Enable rapid deployment and scaling
- Simplify development workflows
The installation process varies by operating system, but the core concepts remain the same. Once installed, Docker provides a consistent interface for managing containers across all platforms.
This tutorial is part of the SysCook DevOps series. For more advanced topics, continue to the next chapters.