Skip to content

Cleanup RUNs don't reduce image size #26

@SeijiSuenaga

Description

@SeijiSuenaga

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions