You need to run `apt-get install` in a Dockerfile. How do you write it so the layer stays small and cache-safe?
BeginnerScenarioImage design and Dockerfile decisions
Answer
Situation read → Classic layer hygiene question.
What I'd do →
RUN apt-get update && apt-get install -y --no-install-recommends \
curl ca-certificates \
&& rm -rf /var/lib/apt/lists/*
- Chain
updateandinstallin oneRUNso a stale package index is never cached alone. - Use
--no-install-recommends. - Remove
/var/lib/apt/listsin the same layer. - Pin versions for critical packages if reproducibility matters.
Why → Cleanup in a separate RUN leaves the data in the earlier layer.
Interview tip → Mention BuildKit cache mounts as the modern alternative: RUN --mount=type=cache,target=/var/cache/apt.
More on Image design and Dockerfile decisions
A teammate's Node.js image is 1.2 GB. How do you approach shrinking it?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