Yazılara Dön

Üretim İçin Docker: En İyi Uygulamalar ve Dağıtım Stratejileri

Samet KocaSamet Koca
DevOps05-04-2026
15 min read
Üretim İçin Docker: En İyi Uygulamalar ve Dağıtım Stratejileri

Docker Production Deployment

Docker has revolutionized how we deploy applications. This guide covers best practices for containerizing applications and deploying them to production environments.

1. Dockerfile Best Practices

A well-optimized Dockerfile is the foundation of efficient containerization:

Multi-Stage Builds

# Build stage
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production

# Production stage
FROM node:18-alpine AS production
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

Security Best Practices

  • Use non-root users
  • Scan images for vulnerabilities
  • Keep base images updated
  • Minimize attack surface

2. Container Orchestration

For production deployments, container orchestration is essential:

Docker Compose for Development

version: '3.8'
services:
  app:
    build: .
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
    depends_on:
      - db
    networks:
      - app-network
  
  db:
    image: postgres:15
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - app-network

volumes:
  postgres_data:

networks:
  app-network:
    driver: bridge

Kubernetes Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: myapp:latest
        ports:
        - containerPort: 3000
        resources:
          requests:
            memory: "256Mi"
            cpu: "250m"
          limits:
            memory: "512Mi"
            cpu: "500m"

3. Environment Management

Proper environment management is crucial for production deployments:

Environment Variables

  • Use .env files for different environments
  • Never commit secrets to version control
  • Use Docker secrets or Kubernetes secrets
  • Validate environment variables at startup

Configuration Management

# docker-compose.prod.yml
version: '3.8'
services:
  app:
    build: .
    environment:
      - NODE_ENV=production
      - DATABASE_URL=${DATABASE_URL}
      - REDIS_URL=${REDIS_URL}
    env_file:
      - .env.production

4. Monitoring and Logging

Comprehensive monitoring is essential for production containers:

Health Checks

# In Dockerfile
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3   CMD curl -f http://localhost:3000/health || exit 1

Logging Strategy

  • Use structured logging (JSON format)
  • Implement log aggregation (ELK stack)
  • Set appropriate log levels
  • Rotate logs to prevent disk space issues

5. Security Considerations

Security should be a top priority in production deployments:

Image Security

  • Scan images for vulnerabilities
  • Use minimal base images
  • Keep images updated
  • Implement image signing

Runtime Security

  • Run containers as non-root users
  • Implement resource limits
  • Use read-only filesystems where possible
  • Implement network policies

6. Performance Optimization

Optimizing container performance improves resource utilization:

Resource Management

# Set resource limits
docker run -d   --name myapp   --memory=512m   --cpus=1.0   myapp:latest

Image Optimization

  • Use multi-stage builds to reduce image size
  • Remove unnecessary files and dependencies
  • Use .dockerignore to exclude files
  • Optimize layer caching

7. CI/CD Integration

Automating the build and deployment process ensures consistency:

GitHub Actions Example

name: Build and Deploy
on:
  push:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    
    - name: Build Docker image
      run: docker build -t myapp:${{ github.sha }} .
    
    - name: Push to registry
      run: |
        docker tag myapp:${{ github.sha }} registry/myapp:${{ github.sha }}
        docker push registry/myapp:${{ github.sha }}
    
    - name: Deploy to production
      run: |
        kubectl set image deployment/myapp myapp=registry/myapp:${{ github.sha }}

8. Backup and Disaster Recovery

Having a solid backup and recovery strategy is essential:

Data Backup

  • Backup persistent volumes regularly
  • Test backup restoration procedures
  • Store backups in multiple locations
  • Document recovery procedures

Disaster Recovery

  • Have multiple deployment environments
  • Implement automated failover
  • Test disaster recovery procedures
  • Maintain runbooks for common issues

Conclusion

Docker production deployment requires careful planning and attention to detail. By following these best practices, you can create robust, scalable, and secure containerized applications.

Remember that containerization is not just about packaging applications—it's about creating a consistent, reliable deployment strategy that scales with your business needs.