Docker Containerization: From Development to Production
Learn to containerize applications effectively with Docker best practices and patterns.
DevOps Engineer
Docker Containerization: From Development to Production
Docker has become essential for modern development. This guide covers everything from basics to production deployment.
Why Docker?
Benefits
- Consistency: Same environment everywhere
- Isolation: No dependency conflicts
- Portability: Run anywhere Docker runs
- Efficiency: Lighter than VMs
- Scalability: Easy horizontal scaling
Dockerfile Best Practices
Multi-Stage Builds
Keep images small:
# Build stage
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
Production stage
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
CMD ["node", "dist/index.js"]
Layer Caching
Order commands strategically:
# Dependencies change less often - cache these
COPY package*.json ./
RUN npm ci
Source code changes frequently
COPY . .
Security
Run as non-root:
RUN addgroup -S app && adduser -S app -G app
USER app
Docker Compose
Development Setup
version: '3.8'
services:
app:
build: .
volumes:
- .:/app
- /app/node_modules
ports:
- "3000:3000"
environment:
- DATABASE_URL=postgres://postgres:password@db:5432/app
depends_on:
- db
db:
image: postgres:15-alpine
volumes:
- postgres_data:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=password
volumes: postgres_data:
Production Considerations
Health Checks
HEALTHCHECK --interval=30s --timeout=3s --retries=3 CMD curl -f http://localhost:3000/health || exit 1
Resource Limits
services:
app:
deploy:
resources:
limits:
cpus: '0.5'
memory: 512M
Logging
Use JSON logging for aggregation:
{"level":"info","message":"Server started","port":3000}
Networking
Bridge Networks
Create isolated networks:
networks:
backend:
driver: bridge
frontend:
driver: bridge
Conclusion
Docker simplifies development and deployment. Master these patterns to build reliable, scalable containerized applications.
DevOps Engineer
DevOps engineer and cloud infrastructure specialist. Kubernetes certified. Writes about CI/CD, containerization, and platform engineering.
Related Articles
Kubernetes in Production: Essential Best Practices
Battle-tested strategies for running Kubernetes clusters in production environments.
How AI is Transforming Software Development
Explore the ways artificial intelligence is changing how we write, test, and deploy code.
Git Workflows for High-Performing Teams
Learn Git branching strategies and collaboration patterns that help teams ship faster.