Docker
devopsdockerGuideAdvanced
Docker Production Best Practices
Comprehensive checklist for deploying Docker containers into production, covering image size optimization, security, logging, graceful shutdown, and orchestration readiness.
Verified: 2026-09-30
Tested on:docker 27.x
Platforms:
linuxmacoswindows
#docker#production#best-practices#devops
Deploying containers to production requires strict adherence to security, reliability, reproducibility, and observability standards.
Production Checklist
1. Minimal & Specific Base Images
- Pin exact major and minor tags (e.g.
node:22.11.0-alpine3.20rather thannode:latest). - Prefer minimal base images like Alpine Linux or Google Distroless to eliminate attack surface and bloated dependencies.
2. Multi-Stage Builds & Dependency Pruning
- Separate build tools, TypeScript compilers, and test runners into builder stages.
- Ship only compiled production bundles and production dependencies (
npm ci --omit=dev).
3. Non-Root Execution
- Ensure container processes execute as an unprivileged user (
USER nodeor custom UID).
4. Forward Linux Signals & Graceful Termination (PID 1)
- Always use the Exec form for
CMD(CMD ["node", "dist/server.js"]). - Implement
SIGTERMandSIGINThandlers in your application code to finish in-flight requests and close database connections before exiting.
// Server graceful shutdown
process.on('SIGTERM', async () => {
console.log('Received SIGTERM, shutting down gracefully...');
await server.close();
await db.disconnect();
process.exit(0);
});5. Health Checks & Readiness Probes
- Add a
HEALTHCHECKinstruction to your Dockerfile or compose file so orchestrators (Docker Swarm, Kubernetes, AWS ECS) can replace deadlocked containers.
6. Resource Limits
- Always define CPU and memory limits (
--memory,--cpus) to prevent rogue containers from degrading host stability.
7. Structured Logging
- Log strictly to
stdoutandstderrformatted as JSON for ingestion by central log management pipelines.
Related Topics
Docker Security Basics & Hardening
Complete guide to container security, non-root users, Linux capabilities, read-only root filesystems, secret management, and vulnerability scanning.
npm: The Node Package Manager
Complete guide to npm, the default package manager for Node.js, covering the npm registry, CLI fundamentals, global vs local packages, and configuration.