Docker

DevOpscontainersDockervirtualizationCI/CDmicroservicescloudinfrastructure

DevOps Tool

Docker

Overview

Docker is a pioneering containerization platform that packages applications and their dependencies into containers, providing consistent execution environments across different systems.

Details

Docker is an open-source containerization platform developed by Solomon Hykes and dotCloud in 2013. It revolutionized application deployment by providing a "run anywhere" solution that is lighter and faster than traditional virtual machines. Docker containers bundle application code with all necessary dependencies (libraries, system tools, settings) into a single package, ensuring consistent behavior from development to production environments. Built on Linux Container (LXC) technology, Docker utilizes namespaces, cgroups, and Union File Systems for process isolation and resource management. The comprehensive ecosystem includes Docker Hub, Docker Desktop, Docker Compose, and more, making it widely adopted by companies like GitLab, Spotify, ING, and PayPal. As the de facto standard for container technology, Docker has accelerated DevOps culture adoption and the proliferation of microservices architectures, establishing itself as a revolutionary platform in modern software development.

Advantages and Disadvantages

Advantages

  • Consistent environments: "Works everywhere" guarantee from development to production
  • Lightweight and fast: Requires fewer resources and faster startup than virtual machines
  • Portability: Multi-platform support (Linux, Windows, macOS)
  • Scalability: Easy horizontal scaling capabilities
  • Rich ecosystem: Comprehensive tools including Docker Hub, Compose, and Swarm
  • DevOps integration: High compatibility with CI/CD pipelines
  • Dependency resolution: Complete isolation of libraries and OS dependencies
  • Development efficiency: Dramatically reduced environment setup time

Disadvantages

  • Learning curve: Requires mastering Dockerfile, networking, and volume management
  • Security concerns: Container isolation level is lower than VMs
  • Persistent data management: Complexity in volume design and backup strategies
  • Monitoring challenges: Container-specific monitoring and log management required
  • Licensing changes: Impact of Docker Desktop monetization on enterprises
  • Linux dependency: Requires virtualization layer on Windows/macOS
  • Orchestration needs: Requires additional tools like Kubernetes for large-scale environments

Key Links

Code Examples

Hello World

# Dockerfile
FROM alpine:latest
RUN echo "Hello, Docker World!" > /hello.txt
CMD ["cat", "/hello.txt"]
# Build and run
docker build -t hello-world .
docker run hello-world

Web Application Containerization

# Node.js application example
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
USER node
CMD ["npm", "start"]
# Build and run
docker build -t my-node-app .
docker run -p 3000:3000 my-node-app

Multi-stage Build

# Build stage
FROM golang:1.20-alpine AS builder
WORKDIR /src
COPY . .
RUN go build -o app main.go

# Runtime stage
FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=builder /src/app /bin/app
CMD ["/bin/app"]

Docker Compose (Multi-container Application)

# docker-compose.yml
version: '3.8'
services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
    depends_on:
      - db
      - redis

  db:
    image: postgres:15
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    volumes:
      - postgres_data:/var/lib/postgresql/data

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"

volumes:
  postgres_data:
# Docker Compose execution
docker-compose up -d
docker-compose logs -f web
docker-compose down

Volume and Network Management

# Create volume
docker volume create my-data

# Create custom network
docker network create my-network

# Run with volume and network
docker run -d \
  --name my-container \
  --network my-network \
  -v my-data:/data \
  nginx:alpine

Docker CLI Basic Operations

# Image management
docker images                    # List images
docker pull nginx:alpine         # Download image
docker rmi nginx:alpine          # Remove image

# Container management
docker ps                        # List running containers
docker ps -a                     # List all containers
docker stop container_name       # Stop container
docker rm container_name         # Remove container

# Debug and monitoring
docker logs container_name       # Show logs
docker exec -it container_name sh # Container shell access
docker stats                     # Resource usage
docker inspect container_name    # Detailed information