What is the most light weight base image I can use to build a Dockerfile?

Jayabalan Bala picture Jayabalan Bala · Mar 23, 2017 · Viewed 8.2k times · Source

I usually use a Ubuntu or Arch Linux image but I found out recently that there is an OS called CoreOS specifically for docker containers.

As I am new to docker I am not sure which one would be the best base image to build my Dockerfile. It seems like a silly question but in case if I run lots of microservices on several containers, then the container should be as light as possible.

Answer

BMitch picture BMitch · Mar 23, 2017

This really depends on your requirements:

  1. FROM scratch: if you are able to statically compile your application and don't need any other binaries (libraries, shells, or any other command period), then you are use the completely empty "scratch". You'll see this used as the starting point for the other base images, and it's also found in a lot of pre-compiled Go commands.

  2. Busybox: I consider this less of a base image and more of a convenient utility container. You get a lot of common commands in a very small size. However what you don't get is the general package manager to easily install other components. Current size of this is under 1M.

  3. Alpine: This is docker's take on a streamlined image and it does a good job and being small but also giving you a package manager. However that small size comes at a cost, things like glibc are not included. You will find that many of the official images are based on Alpine, so inside of the container ecosystem, this is a very popular option. Current size of this is around 2M before you start adding packages.

  4. Debian, Ubuntu, and CentOS: These are less of the lightweight base images, each coming in around 50M give or take. But what they lose with size they gain with a large collection of packages you can pull from and lots of people that are testing, fixing bugs, and contributing to things upstream. They also come with a collection of libraries that some applications may expect to be preinstalled.

While that last option is a bit larger, keep in mind that base images should only be pushed over the wire and stored on disk once. After that, unless you change them, any images built on top of them only need to send the manifest that references layers in that base image and the docker engine will see that it already has those layers downloaded. And with the union fs, those layers never need to be copied even if you run 100 containers all pointing back to that image, they each use the same read-only layer on disk for all the image layers and write their changes to the their container specific RW layer.