Dockerfile: Clean Zypper Cache to Reduce Image Size

Zypper cache files inflate image layers and slow distribution. Clean cache after install in the same layer to reduce image size and keep builds predictable.

Problem

You do not run zypper clean after zypper install. This keeps package cache and metadata in the image layer and increases image size.

Description

Zypper cache improves package operations on persistent hosts, but in container images that cache is usually disposable. Leaving it in a build layer adds transfer size, scan time, and storage overhead without adding runtime functionality.

A frequent mistake is cleaning in a separate layer after installation. Layered filesystems keep the earlier cache payload, so the image remains large. Install and clean in a single RUN command to prevent cache artifacts from being committed to the final image.

Related rules: clean YUM package cache, clean DNF package cache, consolidate RUN instructions.

Solution

Run zypper clean in the same layer as installation and keep package-management steps compact, explicit, and deterministic for reproducible container builds.

Problematic code

FROM opensuse/leap:15.3
USER nobody
RUN zypper install -y httpd

Verified code

FROM opensuse/leap:15.3
USER nobody
RUN zypper install -y httpd && zypper clean

Source of the description

SUSE documentation