beginnerImages, Layers, Caching & Container Lifecycle

Image keeps growing across rebuilds despite removing files

A Dockerfile does `RUN apt-get install -y build-essential` then later `RUN rm -rf /var/lib/apt/lists/*` in a separate RUN instruction, but the image size doesn't shrink. Explain why, and the fix. Expected reasoning: each RUN is its own layer; deleting a file in a later layer just adds a 'deletion marker' on top — the original layer with the large files is still part of the image and counted toward total size. Fix: combine the install and cleanup into a single RUN, or use a multi-stage build so build-essential never reaches the final image.

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to Cache never hits in CI even though source code didn't change← Back to all Docker questions