Learn how to design and deploy APIs that leverage containers for scalability, portability, and efficient resource utilization.
Containers have revolutionized how we build, deploy, and scale applications. For APIs, containerization offers unprecedented flexibility, scalability, and operational efficiency. This guide explores best practices for building container-native APIs that leverage modern cloud infrastructure.
Container-native APIs are designed from the ground up to leverage containerization benefits. Unlike retrofitted applications, they embrace the container paradigm for maximum efficiency and scalability.
# Multi-stage build for minimal production image FROM node:18-alpine AS builder WORKDIR /app COPY package*.json ./ RUN npm ci --only=production && npm cache clean --force FROM node:18-alpine AS runner WORKDIR /app # Create non-root user RUN addgroup --system --gid 1001 nodejs RUN adduser --system --uid 1001 nextjs # Copy built application COPY --from=builder /app/node_modules ./node_modules COPY --chown=nextjs:nodejs . . # Security and performance optimizations RUN npm prune --production USER nextjs EXPOSE 3000 ENV NODE_ENV=production ENV PORT=3000 # Health check HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ CMD curl -f http://localhost:3000/health || exit 1 CMD ["node", "server.js"]
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
labels:
app: api
spec:
replicas: 3
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
containers:
- name: api
image: myregistry/api:v1.0.0
ports:
- containerPort: 3000
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
livenessProbe:
httpGet:
path: /health
port: 3000
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 3000
initialDelaySeconds: 5
periodSeconds: 5