ngseasy
ngseasy copied to clipboard
Cleanup RUNs don't reduce image size
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 thanks for the tip