Interview Prep/Docker/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?

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/*
  1. Chain update and install in one RUN so a stale package index is never cached alone.
  2. Use --no-install-recommends.
  3. Remove /var/lib/apt/lists in the same layer.
  4. 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.

All Docker questions Roadmap: Docker & containers

More on Image design and Dockerfile decisions