Hasty Briefsbeta

Bilingual

We reduced a real Node.js production Docker image from 1.2GB to 78MB

4 hours ago
  • #containerization
  • #docker-optimization
  • #nodejs
  • Switch from a full Debian-based Node image to a slim variant (node:22-slim) to reduce image size from 1.21GB to 412MB.
  • Use a .dockerignore file to exclude unnecessary files like node_modules and .env, reducing size to 388MB and improving security and rebuild speed.
  • Implement a multi-stage build to separate build and runtime environments, reducing size to 198MB by pruning dev dependencies after building.
  • Optimize layer caching by copying package.json first and running npm ci before copying other files, speeding up rebuilds by 5x when only code changes.
  • Switch the runtime stage to Alpine (node:22-alpine) to further reduce size to 96MB, but be cautious with native modules that may require compatibility adjustments.
  • Use Google's distroless image for the runtime stage to reduce size to 78MB, enhancing security by removing shells and package managers, though debugging requires a debug tag.