Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup RUNs don't reduce image size #26

Open
SeijiSuenaga opened this issue Aug 26, 2016 · 1 comment
Open

Cleanup RUNs don't reduce image size #26

SeijiSuenaga opened this issue Aug 26, 2016 · 1 comment

Comments

@SeijiSuenaga
Copy link

SeijiSuenaga commented Aug 26, 2016

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
@snewhouse
Copy link
Contributor

@SeijiSuenaga thanks for the tip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants