Softechinfra
Technology

Docker & Kubernetes Deployment: Production Best Practices

Master Docker and Kubernetes deployments. Learn Dockerfile optimization, K8s manifests, deployment strategies, and monitoring.

Softechinfra EditorialSoftechinfra Editorial
January 20, 202611 min read
Docker & Kubernetes Deployment: Production Best Practices

Docker and Kubernetes have become the standard for deploying modern applications. Our DevOps team at Softechinfra uses these technologies extensively for production deployments.

84%
K8s Adoption Rate
50%
Deployment Faster
78%
Use Containers
3x
Resource Efficiency

Docker Fundamentals

💡 Dockerfile Best Practices

Use specific versions, multi-stage builds, and non-root users for secure, optimized containers.

Dockerfile Best Practices

dockerfile
# Use specific version
FROM node:18-alpine

# Set working directory
WORKDIR /app

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

# Install dependencies
RUN npm ci --only=production

# Copy source code
COPY. .

# Build application
RUN npm run build

# Use non-root user
USER node

# Expose port
EXPOSE 3000

# Start application
CMD ["node", "dist/index.js"]

Multi-Stage Builds

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

# Production stage
FROM node:18-alpine
WORKDIR /app
COPY --from=builder /app/dist./dist
COPY --from=builder /app/node_modules./node_modules
EXPOSE 3000
CMD ["node", "dist/index.js"]

Image Optimization

  • Use Alpine images
  • Multi-stage builds
  • .dockerignore file
  • Layer caching
  • Security scanning
  • Kubernetes Essentials

    Deployment

    yaml
    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: my-app:1.0.0
            ports:
            - containerPort: 3000
            resources:
              requests:
                memory: "128Mi"
                cpu: "100m"
              limits:
                memory: "256Mi"
                cpu: "200m"
            livenessProbe:
              httpGet:
                path: /health
                port: 3000
              initialDelaySeconds: 30
            readinessProbe:
              httpGet:
                path: /ready
                port: 3000
              initialDelaySeconds: 5

    Service

    yaml
    apiVersion: v1
    kind: Service
    metadata:
      name: my-app
    spec:
      selector:
        app: my-app
      ports:
      - port: 80
        targetPort: 3000
      type: ClusterIP

    Ingress

    yaml
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: my-app
      annotations:
        nginx.ingress.kubernetes.io/ssl-redirect: "true"
    spec:
      rules:
      - host: app.example.com
        http:
          paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: my-app
                port:
                  number: 80
      tls:
      - hosts:
        - app.example.com
        secretName: app-tls

    Configuration Management

    ConfigMaps

    yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: my-app-config
    data:
      DATABASE_HOST: "postgres.default.svc"
      LOG_LEVEL: "info"

    Secrets

    yaml
    apiVersion: v1
    kind: Secret
    metadata:
      name: my-app-secrets
    type: Opaque
    data:
      DATABASE_PASSWORD: base64encodedvalue

    Using in Deployments

    yaml
    containers:
    - name: my-app
      envFrom:
      - configMapRef:
          name: my-app-config
      - secretRef:
          name: my-app-secrets

    Deployment Strategies

    Rolling Update

    Default strategy, gradually replaces pods.

    Blue-Green

    Deploy new version alongside old, switch traffic.

    Canary

    Route percentage of traffic to new version.

    yaml
    # Using Nginx Ingress canary
    metadata:
      annotations:
        nginx.ingress.kubernetes.io/canary: "true"
        nginx.ingress.kubernetes.io/canary-weight: "20"

    Monitoring

    Health Checks

    Liveness Probe: Is the application running? Readiness Probe: Is it ready to receive traffic?

    Metrics

  • Prometheus for metrics
  • Grafana for dashboards
  • Alert Manager for alerts
  • Logging

  • Centralized logging (EFK/ELK)
  • Structured logs
  • Log aggregation
  • Best Practices

    Security

  • Non-root containers
  • Read-only filesystem
  • Network policies
  • Image scanning
  • Pod security policies
  • Resource Management

  • Always set requests and limits
  • Right-size based on usage
  • Horizontal Pod Autoscaler
  • Reliability

  • Multiple replicas
  • Pod disruption budgets
  • Anti-affinity rules
  • Health checks
  • Conclusion

    "Docker and Kubernetes provide powerful primitives for deploying applications. Start with fundamentals and build expertise over time."
    — Hrishikesh Baidya, Lead Developer

    Container orchestration is essential for modern deployments. We've deployed containerized applications for projects like TalkDrill using Kubernetes with our development expertise. Check our Kubernetes cost optimization guide for more tips.

    Need Help with Container Deployments?

    Our DevOps team designs and implements container strategies with Docker and Kubernetes for scalable, reliable production deployments.

    Get DevOps Consultation
    Tags:
    DockerKubernetesDevOpsContainersCloud Native
    Share this post:
    Softechinfra Editorial

    Softechinfra Editorial

    Articles published by Softechinfra without a personal byline.

    How we pick topics, use AI, check claims and correct mistakes: our editorial policy.