Back to journal
DevOps10 min readFebruary 5, 2024

Docker & Kubernetes: Production Deployment Guide

Learn how to containerize your applications and deploy them at scale using Docker and Kubernetes.

#Docker#Kubernetes#DevOps

Containerization has revolutionized how we deploy applications. This guide covers Docker and Kubernetes for production environments.

Docker Basics

Writing a Dockerfile

# Use specific versions, not latest
FROM node:20-alpine

WORKDIR /app

# Copy package files first for better caching
COPY package*.json ./
RUN npm ci --only=production

COPY . .

# Run as non-root user
USER node

EXPOSE 3000
CMD ["node", "server.js"]

Docker Compose for Development

version: '3.8'
services:
  app:
    build: .
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=development
    volumes:
      - .:/app
      - /app/node_modules
    depends_on:
      - db
      - redis

  db:
    image: postgres:15-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: secret

  redis:
    image: redis:7-alpine

volumes:
  postgres_data:

Kubernetes for Production

Deployment Manifest

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: myregistry/my-app:v1.0.0
        ports:
        - containerPort: 3000
        resources:
          limits:
            memory: "512Mi"
            cpu: "500m"
          requests:
            memory: "256Mi"
            cpu: "200m"
        env:
        - name: NODE_ENV
          value: "production"
---
apiVersion: v1
kind: Service
metadata:
  name: my-app
spec:
  selector:
    app: my-app
  ports:
  - port: 80
    targetPort: 3000
  type: LoadBalancer

Best Practices

  • Use multi-stage builds to reduce image size
  • Implement health checks (liveness/readiness)
  • Set resource limits to prevent resource exhaustion
  • Use secrets for sensitive data
  • Implement proper logging and monitoring

Conclusion

Docker and Kubernetes provide the foundation for reliable, scalable deployments. Start with Docker Compose for development, then migrate to Kubernetes for production.

If the note connects to your work

If the project needs a clearer technical read, send a brief.

Send a brief