Squash a Bloated Container Image to Remove Waste and Leaked Credentials
A colleague containerized the team's Python microservice. During the build,
they installed build-essential and libpq-dev
to compile native extensions, and copied in a pip.conf file with
credentials for the company's private package registry.
Trying to keep the image clean, they later removed both the build tools and the credentials file from the final container filesystem. But did that actually make the image safe and compact?
Explore the image
The image is available at ghcr.io/iximiuz/labs/bloated/app:v1.0.0.
It has already been pulled for you, so go ahead and inspect it:
docker history ghcr.io/iximiuz/labs/bloated/app:v1.0.0
Two things should catch your attention:
- A ~400 MB layer from
apt-get install -y build-essential libpq-dev- later "removed" withapt-get purge, but the original layer is still part of the image. - A layer from
COPY pip.conf /root/.config/...- later "deleted" withrm, but anyone who pulls the image can extract the intermediate layers and recover the private registry token.
Squash the image
Your task is to squash (flatten) the image into a clean, compact version where:
- The runtime filesystem is preserved - every file that exists in the original container must also exist in the squashed one (and the app must still work).
- The image is significantly smaller (the leftover build tools are truly gone).
- No credentials appear in any image layer.
Push the squashed image to the playground's container registry as registry.iximiuz.com/squashed/app:v1.0.0.
Hint 1 - How to flatten image layers
Squashing means collapsing all image layers into a single one that represents only the final filesystem state. You can do it with "bare" Docker commands, but it might be really tedious and error-prone.
Luckly, there are tools like crane that can do this for you.
Hint 2 - Using 'crane flatten'
crane (preinstalled in this playground) can flatten an image in one command -
crane flatten.
Beware that it operates on registry images, so you'll need the source image in a
registry that crane can both pull from and push to.