CodePark/ questions / Multi-stage builds & image size
START PRACTICINGPRACTICE
Backend question · Docker › Multi-stage builds & image size

An image is 900 MB. A teammate adds RUN rm -rf /app/node_modules/.cache /tmp/* as the last instruction. What happens to the image size?

It stays about the same, because the files still exist in the earlier layers

The image stays about the same size, because the files still exist in the earlier layers. Image layers only add. The new layer only records that the files are deleted. The earlier layer still contains the files, and Docker still ships it. So "It shrinks by the size of the deleted files" is wrong.

To really save space:

• delete the files in the same
RUN instruction that created them, or
• use a multi-stage build and copy only the files you need.