Review dnf installation cache cleanup

Install packages and clean the dnf cache in the same RUN instruction.

Description

dnf install can leave package metadata and cached downloads in the image layer.

Potential impact

A larger layer takes more space and longer to pull, even if you delete the cache in a later RUN.

Remediation

Run dnf clean all after a successful install in the same RUN. Check the final image size and keep installed packages up to date.

Examples

The Fedora 40 example uses nginx. For production, choose a supported base with a repository that provides it.

Before

dockerfile
FROM fedora:40

RUN dnf install -y nginx

After

dockerfile
FROM fedora:40

RUN dnf install -y nginx \
    && dnf clean all

References