Bifalgorithm post
Your hello world container is one gigabyte bigger than the app inside it Here's why your docker file starts with from node copies everything in and runs npm install That pulls in a full operating system a compiler build tools in every dev dependency The app is tiny the box around it is huge First an image is just layers every instruction stacks one read-only layer on the last Copy your whole folder before you install and one changed file rebuilds everything Now the real fix multi-stage builds do the heavy work in a first stage install compile build Then start a fresh tiny stage and copy and only the finished artifact the compiler the dev dependencies the source all left behind They never ship two more wins use a slim or alpine base and add a dot docker ignore So node modules never get copied put it together in one gigabyte becomes around 80 megabytes Faster pulls faster deploys and a smaller attack surface Your image should ship your app not your whole laptop follow for more dev wins
Your hello world container is one gigabyte bigger than the app inside it Here's why your docker file starts with from node copies everything in and runs npm in…
Ideas in this flowchart
- Docker Image Optimization
- Problem: Large Docker Image
- Dockerfile Structure
- FROM node
- COPY Everything
- RUN npm install
- Includes Full OS & Dev Dependencies
- Image Layer Structure
- Image is Layers
- Each Instruction Adds Layer
- Rebuilds on File Change
- Solution: Multi-Stage Builds
- First Stage: Heavy Work
- Install, Compile, Build
- Second Stage: Fresh Start
- Copy Finished Artifact Only
- Exclude Compiler & Dev Dependencies
- Additional Optimizations
- Use Slim or Alpine Base
- Add .dockerignore
- Exclude node_modules
- Benefits
- Smaller Image Size
- Faster Pulls & Deploys
- Reduced Attack Surface