Building Container Images Multi-stage builds and image optimization
Multi-Stage Dockerfiles
| Stage | Purpose | Example Base |
|---|
| Build | Compile/package application | golang:1.21, node:18 |
| Final | Minimal runtime image | scratch, distroless, alpine |
Use COPY --from=builder /app/binary /app/binary to copy only the compiled artifact into the final stage.
Workload Resources
- Deployment — stateless apps with rolling updates
- StatefulSet — stateful apps with stable network IDs and storage
- DaemonSet — one pod per node (logging, monitoring agents)
- Job — run-to-completion batch tasks
- CronJob — scheduled jobs with cron syntax
Multi-Container Pod Patterns
| Pattern | Purpose | Example |
|---|
| Sidecar | Extend/enhance main container | Log shipper, proxy |
| Init Container | Setup before main starts | DB migration, config fetch |
| Adapter | Normalize output | Metrics format converter |
| Ambassador | Proxy external access | Database proxy |