Pertemuan 10: Containerization dengan Docker & Kubernetes
Progress: 0%
Containerization Workflow
Source Code
Dockerfile
Container Image
Container Registry
Kubernetes Cluster
Tujuan Pembelajaran
Memahami Containerization
Konsep containerization dan perbedaannya dengan virtualisasi tradisional
Membangun Docker Images
Membuat Docker image untuk aplikasi multi-tier dengan Docker Compose
Kubernetes Orchestration
Deploy aplikasi containerized ke cluster Kubernetes dengan fitur advanced
Konsep Dasar Containerization
Fundamental
Architecture
Virtual Machine
Size: GBs
Boot Time: Minutes
Isolation: Hardware Level
Performance: Overhead
Density: Low
Container
Size: MBs
Boot Time: Seconds
Isolation: OS Level
Performance: Native
Density: High
Portability
"Build once, run anywhere" - konsisten di semua environment
- Development → Production
- On-premises → Cloud
- Multi-cloud deployment
- No environment-specific issues
Scalability
Rapid scaling dan deployment dengan density tinggi
- Microservices architecture
- Horizontal scaling
- Resource efficiency
- Fast startup times
DevOps Enablement
CI/CD integration dan modern development practices
- Infrastructure as Code
- Automated deployments
- Version control for infrastructure
- Rollback capabilities
Docker Containerization
Hands-on
Docker Compose
Multi-tier Application Architecture
Presentation Tier
React Frontend
Application Tier
Python Flask API
Data Tier
MySQL Database
Redis Cache
# backend/Dockerfile
FROM python:3.9-slim
WORKDIR /app
# Install system dependencies
RUN apt-get update && apt-get install -y \
default-libmysqlclient-dev \
build-essential \
pkg-config \
&& rm -rf /var/lib/apt/lists/*
# Copy requirements and install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy application code
COPY . .
# Expose port
EXPOSE 5000
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl -f http://localhost:5000/health || exit 1
# Run application
CMD ["python", "app.py"]
# frontend/Dockerfile
FROM node:18-alpine as build
WORKDIR /app
# Copy package files
COPY package.json package-lock.json ./
RUN npm ci --only=production
# Copy source code
COPY . .
# Build application
RUN npm run build
# Production stage
FROM nginx:alpine
# Copy built application
COPY --from=build /app/build /usr/share/nginx/html
# Copy nginx configuration
COPY nginx.conf /etc/nginx/nginx.conf
# Expose port
EXPOSE 80
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl -f http://localhost:80/ || exit 1
CMD ["nginx", "-g", "daemon off;"]
# docker-compose.yml
version: '3.8'
services:
# MySQL Database
database:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: ecommerce
MYSQL_USER: appuser
MYSQL_PASSWORD: apppassword
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
timeout: 20s
retries: 10
# Backend API
backend:
build: ./backend
environment:
DB_HOST: database
DB_USER: root
DB_PASSWORD: rootpassword
DB_NAME: ecommerce
DB_PORT: 3306
ports:
- "5000:5000"
depends_on:
database:
condition: service_healthy
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:5000/health"]
interval: 30s
timeout: 10s
retries: 3
# Frontend Application
frontend:
build: ./frontend
ports:
- "80:80"
depends_on:
- backend
volumes:
db_data:
#!/bin/bash
# deploy-docker.sh
echo "Building and deploying multi-container application..."
# Build images
docker-compose build
# Start services
docker-compose up -d
# Check service status
docker-compose ps
# View logs
docker-compose logs -f backend
# Test application
echo "Testing application..."
curl http://localhost:80
curl http://localhost:5000/api/products
echo "Application deployed successfully!"
echo "Frontend: http://localhost:80"
echo "Backend API: http://localhost:5000"
echo "Database: localhost:3306"