# Creating super small docker images

|   Source

Docker containers are lighter than virtual machines but in many cases images are way bigger than they ought to be. For example the official docker python image is approximately 900MB in size and this is only for the python runtime with no external libraries installed.

Python itself is not small, a typical python installation needs close to 100 MB once uncompressed on the disk. Of course one could imagine that here are many files included that aren't needed in most of the usual cases (like the turtle module). Is it possible to create a smaller python docker image?

The answer is YES; if you now do a docker pull elyase/staticpython you will get a working python image with only 8.5 MB in size.

#### Looking for the smallest base image¶

Docker images are built from a base image. If we want the get a final small container this should be the first optimization target. Lets take a look at some popular base images:

Linux Tag Size(MB)
fedora heisenbug 372
ubuntu 14.04 195
debian wheezy 85
busybox latest 2.5

Busybox is the clear winner but the small size comes with its own tradeoffs. Busybox is a very minimal distro, it doesn't have a package manager or a gcc compiler, so compilation from scratch of new packages is also difficult.

Fortunately Jeff Lindsay has created a busybox based image with the opkg package installer pre-configured with OpenWRT repositories. You don't really get recent versions or a great variety of packages, in fact you don't even get normal GNU versions of the UNIX utilities which brings some difficulties. For example wget doesn't work with https sites and curl has outdated certificates. Also uncompressing files is different as bzip and tar have some common command line options missing. But with some trial and error it can be enough to create a working python environment.

#### Looking for the smallest python¶

The first one I found was eGenix PyRun: "the one file Python runtime environment". The executable only needs 11MB for Python 2 and 13MB for Python 3. They achieve this impressive size by "freezing" the whole standard library. Also some less used modules like tkinter have been stripped out. In general everything should run like in a standard python distribution.

But finally I went for StaticPython which is even smaller at approximately 6MB. StaticPython is similar to PyRun, except that StaticPython doesn't let you compile C extensions. Also and PyRun needs several libraries (e.g. glibc, OpenSSL, zlib, SQLite, bzip2) installed on the host, StaticPython on the other side runs even if it's the only file on the filesystem. That's great packaging! You can see the resulting image at the Docker Registry. For example:

#### What about pip, numpy, etc.¶

While pyrun is compatible with setuptools, easy_install, pip, I decided to only pack the python executable in order to achieve the smallest possible size but I might make a full pip compatible image in the future. For more fully featured installations needing additional dependencies I created an additional image using the Anaconda Python Distribution.

#### Anaconda + Busybox¶

This one was somewhat more complex to get working because I had to adapt the full miniconda installer to work in the constrained busybox environment. The Dockerfile is quite simple:

FROM progrium/busybox
MAINTAINER Yaser Martinez Palenzuela
RUN opkg-install bash bzip2