Skip to content
londondaintta edited this page Dec 13, 2023 · 8 revisions

Delete the apt-get lists after installing something.

Problematic code:

RUN apt-get update && apt-get install --no-install-recommends -y python

Correct code:

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

Rationale:

https://docs.docker.com/develop/develop-images/instructions/#apt-get

In addition, when you clean up the apt cache by removing /var/lib/apt/lists it reduces the image size, since the apt cache isn’t stored in a layer. Since the RUN statement starts with apt-get update, the package cache is always refreshed prior to apt-get install.

Notes:

Clean up must be performed in the same RUN step, otherwise it will affect image size.

Clone this wiki locally