← Back to all fixes

Docker image is several GB — much larger than expected

The Issue

`docker images` shows your app image is 2.5 GB. Pulls and deploys are slow.

Root Cause

Common causes: using a full OS base image instead of alpine, including dev dependencies, not cleaning up apt/apk cache, COPY-ing unnecessary files like node_modules or .git.

How to Fix
  1. Switch base image to alpine variant: `FROM node:20-alpine` instead of `FROM node:20`
  2. Use multi-stage builds: build in one stage, copy only the output to a clean stage
  3. Add `.dockerignore` to exclude `node_modules`, `.git`, `*.log`, `deploy/` etc.
  4. After apt-get install, always run: `rm -rf /var/lib/apt/lists/*` in the same RUN layer
  5. Check what is bloating the image: `docker history myapp:latest`