Skip to content

John Lam's Blog

2022-01-06

Optimizing Docker image size

There's a nice blog on optimizing Docker image size. I learned about tools that I haven't known about before, especially dive. This lets you troubleshoot your Docker images and even tells you when you have files duplicated in different layers in your image. It's a great way to discover waste in your images.

Animated gif showing dive

The HN thread also has a bunch of useful tips for managing layers:

You can use --squash to remove all intermediate layers

The Docker BuildKit tool supports heredocs!

RUN <<EOF
apt-get update
apt-get upgrade -y
apt-get install -y ...
EOF

There's a tool called stargz (seekable tar.gz format) which greatly improves startup time in container images by lazy loading files stargz snapshotter

Look at how it improves startup time for python-3.7 image:

This FOSDEM 2021 presentation is a good overview of stargz

This is built on top of the Google CRFS (Container Registry Filesystem) project which is a FUSE filesystem that lets you mount a container image served directly from a container registry without pulling it locally first. The linked repo has a fantastic README that explains how it all works.