A teammate's Node.js image is 1.2 GB. How do you approach shrinking it?
BeginnerScenarioImage design and Dockerfile decisions
Answer
Situation read → Large image usually means a fat base, dev dependencies, build tools and a copied build context.
What I'd do →
- Run
docker history --no-trunc myapp:latestanddocker image lsto see which layers are big. - Switch from
node:22tonode:22-slimornode:22-alpine(test musl compatibility first). - Use
npm ci --omit=devand add a.dockerignorewithnode_modules,.git,*.md. - Move to multi-stage so build tooling never ships.
Why → Every layer is additive; deleting files in a later layer does not shrink the image.
Interview tip → Say you measure with docker history or dive before optimizing.
More on Image design and Dockerfile decisions
You need to run `apt-get install` in a Dockerfile. How do you write it so the layer stays small and cache-safe?BeginnerWhen would you pick `COPY` over `ADD`?BeginnerA service must run a startup script and then the app. Should you use `CMD`, `ENTRYPOINT`, or both?BeginnerYour Docker build re-installs all Python dependencies on every code change. Fix it.BeginnerProduct asks for a container that runs both nginx and a Python app. What do you say?BeginnerYour app ignores `docker stop` and takes exactly 10 seconds to die. Why and what do you change?Intermediate