How to use apt install correctly in your Dockerfile
This is the correct way to use apt install
in your Dockerfile
:
ENV DEBIAN_FRONTEND=noninteractive
RUN apt update && apt install -y PACKAGE && rm -rf /var/lib/apt/lists/*
Key takeaways:
- Set
DEBIAN_FRONTEND=noninteractive
to prevent some packages from prompting interactive input (tzdata
for example), which leads to indefinite waiting for an user input - Run
apt update
before theinstall
command to fetch the current package lists apt install
with-y
to preventapt
from asking you if you really want to install the packagesrm -rf /var/lib/apt/lists/*
after theinstall
command in order ot prevent the cachedapt
lists (which are fetched byapt update
) from ending up in the container image- All of that in one command joined by
&&
in order to preventdocker
from building separate layers for each part of the command (and to prevent it from first storing/var/lib/apt/lists
in one layer and then delete it in another layer)