Skip to content
Back to Journal

Stop Shipping Your Entire Kitchen: Optimising Docker Container

Docker image optimisation illustration comparing a 1.28 GB image with a 110 MB image

I had my first introduction to containerisation about 9 years ago. Coming from the world of spinning up VMs as if there was no tomorrow, I was pleasantly surpised and quite fascinated by the incredible abstraction that was now possible with containerisation as a concept, and in particular, how easy Docker made it available as open source offering. One of my earliest projects were to spin up an ubuntu container on a windows laptop, allowing me to explore and uncover operating system internals.

Fast forward a couple of years, I was using Docker quite regularly, creating images from various base images that was available on the Docker hub. The slims to alpines, I had used them all. At one point, I started observing the fact that the images I built ended up being too large. Sometimes, several gigs in size. So much so that, they took longer to be built through CICD, I had to constantly remove images to clear up space, using them on Kubernetes clusters took longer to spawn during a pod after a crash. I had to figure this out! That is when I stumbled upon a brilliant tool..

Enter dive!

Dive is a command-line tool for exploring the layers of a Docker image.

You see, your docker images are basically like a version controlled set of packages stacked one layer upon another. Every single instruction on your Dockerfile abstracts to become a layer in docker that contains everything that was added, removed or modified. Dive, basically, allowed you to inspect exactly that level of abstraction, rather than as one big file system.

Installing on mac is as easy as brew install dive on your terminal and you are ready to go!

Simply type dive <image tag> and you will see an interactive view of the docker image. It makes a few common problems immediately visible.

Finding the bloat

Say, for instance, your dockerfile contains the following instructions:

Dockerfile
RUN apt-get update
RUN apt-get install -y build-essential
RUN pip install -r requirements.txt
COPY . /app

While your container might only be utilising binaries worth a few 100 MBs, the container size itself may end up being several times larger.

Dive can help you reveal why?

You might find some of these:

  • Package manager caches or Python / node caches
  • build dependencies
  • test data, local artefacts, etc.
  • large files created in one layer, deleted in another

Docker images are made of immutable layers. If you add a 500 MB file in one layer and delete it in the next, the final filesystem may no longer show the file, but those bytes can still exist in the underlying image layers.

For example:

Dockerfile
RUN wget huge-file.tar.gz
RUN tar -xzf huge-file.tar.gz
RUN rm huge-file.tar.gz

may continue to carry unnecessary weight compared to:

Dockerfile
RUN wget huge-file.tar.gz \
    && tar -xzf huge-file.tar.gz \
    && rm huge-file.tar.gz

Dive makes this kind of behaviour much easier to spot.

Onto optimisation

Here are some of my learnings over the years that can give you a headstart to making those images leaner:

  1. Use smaller base images. I continue to look for either alpine or slim images, simply because they are already lean to an extent. For instance, FROM python:3.13-slim
  2. Avoid retaining package caches. RUN pip install --no-cache-dir -r requirements.txt
  3. Use.dockerignore to prevent unnecessary local artefacts from entering the build context: .git, *.log, .venv, tests, .idea, etc.
  4. Consider multi-stage builds for containers that might require compilers to be included.

Finally

In my first attempt using dive for image optimisation resulted in me being able to bring down the image size from about 4.5GB to 1.5GB. And, after several more attempts and utilising multi-stage builds, I ended up bringing it further down to around 700MB. This was already a great result. I was quite proud of what I was able to achieve with a simple tool that provided a view into your builds!

Do consider including dive into your development toolset. Sometimes the fastest way to optimise a container is simply understanding what you’re actually shipping.