How to use apt install correctly in your Dockerfile
This is the correct way to use apt install in your Dockerfile:
example-1.dockerfile
RUN apt update && apt install -y PACKAGE && rm -rf /var/lib/apt/lists/*Key takeaways:
- Set
DEBIAN_FRONTEND=noninteractiveto prevent some packages from prompting interactive input (tzdatafor example), which leads to indefinite waiting for an user input - Run
apt updatebefore theinstallcommand to fetch the current package lists apt installwith-yto preventaptfrom asking you if you really want to install the packagesrm -rf /var/lib/apt/lists/*after theinstallcommand in order ot prevent the cachedaptlists (which are fetched byapt update) from ending up in the container image- All of that in one command joined by
&&in order to preventdockerfrom building separate layers for each part of the command (and to prevent it from first storing/var/lib/apt/listsin one layer and then delete it in another layer)
Also see the official guide on Dockerfile best practices
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow