Docker containers provide consistent environments from development to production. Here's how to build efficient, secure Node.js containers.
Multi-Stage Builds#
Optimized Layer Caching#
.dockerignore#
# Dependencies
node_modules
npm-debug.log
# Build artifacts
dist
build
coverage
# Development files
.git
.gitignore
.env*
.editorconfig
.eslintrc*
.prettierrc*
tsconfig.json
# Documentation
README.md
docs
# Tests
__tests__
*.test.js
*.spec.js
jest.config.js
# IDE
.vscode
.idea
# Docker files
Dockerfile*
docker-compose*
.dockerignore
Security Hardening#
Environment Variables#
Development Setup#
Health Checks#
Logging#
Resource Limits#
Graceful Shutdown#
Best Practices#
Image Size:
✓ Use Alpine-based images
✓ Multi-stage builds
✓ Remove dev dependencies
✓ Clean npm cache
Security:
✓ Run as non-root user
✓ Update base images regularly
✓ Scan for vulnerabilities
✓ Don't store secrets in images
Performance:
✓ Optimize layer caching
✓ Use .dockerignore
✓ Set resource limits
✓ Configure health checks
Development:
✓ Use docker-compose
✓ Mount source for hot reload
✓ Match production environment
✓ Use consistent versions
Conclusion#
Docker enables consistent deployments across environments. Use multi-stage builds for smaller images, run as non-root for security, and implement health checks for reliability. The extra setup time pays off in deployment confidence.