Let's assume that you're building a small CakePHP application which requires PHP's intl extension for internationalisation support, and PDO's PostgreSQL driver, for the purposes of this tutorial. To install it, you need to install libicu-dev, which you can see in the small Dockerfile example below.
FROM php:8.3.7-apache-bookworm
RUN apt-get update \
&& apt-get install -y libicu-dev \
&& docker-php-ext-install bcmath intl pdo pdo_mysql
RUN a2enmod rewrite
The essence of the post is to encourage people to remove or clean up package manager caches when building images in Dockerfiles, so that the images are as small as possible.
I'll use APT and APK as examples, showing how to clean them with commands such as apt clean, and rm -rvf.
If you want to learn the essentials of images, containers, Docker, and Docker Compose, so that you can build local development environments and deploy your code, Deploy With Docker Compose is where you need to start.