Node.js with Vite
Best practices for Dockerfile for Node.js with Vite
🐳 Annotated Dockerfile for Node.js with Vite:
🔍 Why these are best practices for Vite:
✅ Static build output optimization
- Vite produces static files in the
dist
directory that are ideal for serving with NGINX - No need for a Node.js runtime in production for client-side applications
- Much smaller final image size and improved security
✅ Multi-stage build approach
- The final image only contains the built assets, not any source code or dependencies
- Build tools, source code, and node_modules are not included in the production image
- Significantly reduces the attack surface and image size
✅ Dependency caching
- Speeds up repeated builds by caching the npm modules
- Improves CI/CD pipeline efficiency
✅ Non-root NGINX configuration
- Runs NGINX as a non-root user for enhanced security
- Properly sets file permissions for the nginx user
🚀 Additional Vite-specific configurations:
Environment Variables in Vite
Vite handles environment variables differently. Only variables prefixed with VITE_
are exposed to client-side code:
Custom NGINX Configuration
For SPAs (Single Page Applications), you might need this NGINX configuration:
Save this as nginx.conf
in your project and uncomment the COPY line in the Dockerfile.
Development Setup with Docker Compose
This approach mounts your local directory for hot-reloading during development.
By following these best practices, your Vite applications will have optimized production builds with minimal container size and maximum security.
Last updated on