Review apt-get package-list cleanup

Install packages and remove their package lists in the same RUN instruction.

Description

apt-get update stores package lists under /var/lib/apt/lists/. They are useful during installation but take up space in the runtime image.

Potential impact

Removing the lists in a later layer leaves the earlier layer just as large.

Remediation

Run apt-get update, apt-get install, and rm -rf /var/lib/apt/lists/* in the same RUN, in that order. If a later step installs more packages, refresh the lists first.

Examples

apt-get clean removes downloaded package archives. Package lists need the cleanup shown below. Manage package versions and security updates separately.

Before

dockerfile
FROM ubuntu:24.04

RUN apt-get update \
    && apt-get install -y --no-install-recommends python3

After

dockerfile
FROM ubuntu:24.04

RUN apt-get update \
    && apt-get install -y --no-install-recommends python3 \
    && rm -rf /var/lib/apt/lists/*

References