Remove Package Manager Caches for Smaller Image Sizes

Keeping images small is a key Docker best practices. But how do you do that when you need to install packages for your application to work? One way is to clean out package manager caches. In this short tutorial, I'm going to show you how to do that with Debian and Alpine Linux base images.

Not sure how to use Docker Compose?

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.

Take a look!


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.


Filed under
DevOps Docker Linux