Your Docker image is 1.5 GB. How would you reduce its size?
Use a smaller base image, a multi-stage build, a .dockerignore, install only production dependencies, chain and clean package installs in one RUN, and inspect layers with docker history or dive to find the heavy ones.
Explain it simply
Start like a doctor: measure before treating. docker history myapp lists each layer with its size; the tool dive shows what each layer adds.
Then apply the usual fixes in order of payoff. (1) Switch from a full node or ubuntu base to a -slim or distroless one. (2) Use a multi-stage build so compilers and dev dependencies stay in the build stage. (3) Add a .dockerignore so .git, node_modules and test data are not copied. (4) Install production dependencies only, such as npm ci --omit=dev or pip install --no-cache-dir. (5) Remove package caches in the same RUN that creates them. (6) Do not copy large datasets or model files into the image; mount or download them at runtime.
Smaller images pull faster in CI and on new nodes, and contain fewer vulnerabilities.
Example
docker images myapp # SIZE column
docker history myapp # size per layer
# Dockerfile tips
# RUN pip install --no-cache-dir -r requirements.txt
# RUN npm ci --omit=devWhat to say in the interview
"I inspect layers with docker history or dive, then switch to a slim base, use multi-stage builds, add a .dockerignore, install only production dependencies, clean caches in the same RUN, and keep big data out of the image."
Common mistakes
- Optimising randomly without checking which layer is actually large.