Optimized Docker Image Size with Multi-Stage Builds
Multi-Stage Builds
Think of this as packing only what you need. Build everything in one stage, then copy just the essentials to the final image. This keeps things simple and small.
Use Slim Base Images
I switched to node:14-slim, which has everything needed to run the app without extras. It made a big difference.
Clean Up as You Go
I removed unnecessary files and packages after installing. Less clutter equals a smaller image!
Skip Dev Dependencies
For production, you only need what’s required to run the app, not build it. So, I left out the development tools.
Try Alpine Images
If you’re aiming to save even more, Alpine images are tiny. They need a bit more setup, but they’re worth it for a super light container.
Reference
https://docs.docker.com/build/building/multi-stage/
https://www.linkedin.com/posts/raghavdua_how-do-multi-stage-builds-reduce-the-size-activity-7223975756249374720-z3Ws
https://www.kubeblogs.com/reduce-docker-image-size/