Why RUN command which deletes files inflates image size?

Deleting files that were added on a previous layer does NOT to reduce the image size. The new layer created will simply not show these files anymore. Any file modified file is also copied onto new layers.

Haing said that, remember that EVERY instruction in a Dockerile (RUN, ADD, ENV, etc) creates a diferent layer. For this reason, the community has adopted the practice of bundling as many linux command lines as possible into one single RUN instruction.

It’s also worth mentioning that even simple linux commands, like touch, chown or chmod can cause files/directories to be replicated into the next layer. An innocent “chown -R” on a directory with hundredds of MB will cause all of its contents to be duplicated. Files are considered to have changed, so they are copied to the next layer.

This is a major concern while building images. I’m glad the “–squash” parameter is on its way and look forward to using it.