Skip to content

delete-apt-lists

Dockerfileperformanceno fixdefault: offdockerfile/hadolint

Reports a RUN that updates the apt package lists and does not delete them. It runs apt-get update or apt update and has no rm of /var/lib/apt/lists after it in the same RUN. apt-get clean does not count, it empties another directory. A RUN with a --mount of type cache or tmpfs on /var/lib/apt/lists is not reported.

apt-get update downloads the package index files into /var/lib/apt/lists, often tens of megabytes that no program needs at run time. They are written into the layer of that RUN, and deleting them in a later RUN hides them but does not make the image smaller, because the earlier layer still holds the files.

To fix, remove them in the same RUN: apt-get update && apt-get install -y ... && rm -rf /var/lib/apt/lists/*.

Reported
...
RUN a | b
FROM debian:12
...
RUN apt-get update
RUN apt-get update && apt-get install -y a
RUN apt update
RUN apt-get update && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y a && rm -fr "/var/lib/apt/lists"
RUN --mount=type=cache,target=/var/lib/apt/lists apt-get update
...
dockerfile:
lint:
rules:
delete-apt-lists: warn # off (default), warn, error

Enabled by the presets: dockerfile/hadolint.

Suppress one line with sudsy-disable-next-line delete-apt-lists in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
hadolint DL3009 partial Only the RUN that runs apt-get update is checked, and any rm of a path under /var/lib/apt/lists in it counts, as hadolint does.