ngseasy icon indicating copy to clipboard operation
ngseasy copied to clipboard

Cleanup RUNs don't reduce image size

Open SeijiSuenaga opened this issue 8 years ago • 1 comments

Just wanted to point out that deleting temporary files in a Dockerfile doesn't reduce image size — unless you delete them in the same RUN that created them.

This is because each RUN command builds a separate image layer. After a layer has been built, any files that it added to the image are "baked in" and will contribute to final image size even if you delete them in a subsequent layer. In such a case, the deletion only serves to make files inaccessible in the running container.

The solution is to execute downloads/installs and cleanups in the same RUN, so only the net result gets baked into the image. Files that were added then removed in the same RUN are completely excluded from the single resulting layer.

RUN curl https://example.com/example.tar.gz | tar xz -C temp \
 && apt-get update \
 && apt-get install -y some-app \
 && some-app temp \
 && apt-get remove -y some-app \
 && apt-get clean \
 && rm -rf temp

SeijiSuenaga avatar Aug 26 '16 02:08 SeijiSuenaga

@SeijiSuenaga thanks for the tip

snewhouse avatar Aug 30 '16 12:08 snewhouse