When I want to optimize the Dockerfile, I need to know the meaning of the base image tag. Like:
3.8.0a4-stretch
, 3.7.3-alpine3.8
, 2.7.16-slim-jessie
. I do some research, and make a
summary of the meaning of the base image tag, and I’ll give you advice about choosing the proper base image.
The Linux distribution
When we use Python base images, like 3.8.0a4-stretch
, The 3.8.0a4
is the Python version, and the stretch
is the Linux distribution.
There are a few notes:
stretch
andjessie
isDebian
release, the former meansDebian 9
, the latter meansDebian 8
alpine
is another Linux distributionslim-jessie
meansDebian 8
but remove some extra files that are normally not necessary within containers: docker-slim
Distribution details
We all have heard the mainstream distribution, like
Debian
,Ubuntu
,Centos
. But as base images, they are very large to takes up a few hundred megabytes of space
Alpine
alpine linux is much smaller than most distribution base images (~5MB), which uses musl libc and BusyBox. It also has their own package managers, better than some images just use busybox
. It’s a good choice to use it as a base images.
Please be noted that:
The main caveat to note is that it does use musl libc instead of glibc and friends, so software will often run into issues depending on the depth of their libc requirements/assumptions.
docker-slim
In the docker-slim, docker-slim
could convert a big ubuntu
image(400MB) to 14MB
.
This image does not contain the common packages contained in the default tag and only contains the minimal packages needed to run python. Unless you are working in an environment where only the python image will be deployed and you have space constraints, we highly recommend using the default image of this repository.
I noticed that the official base images like Python
, Node
, OpenJDK
, PHP
, they all have
slim-stretch
or slim-jessie
as the base image tag. So maybe this is a good sign to use Debian
.
How to choose base image
A survey
From this picture, The alpine
is widely used, and even larger than the other images occupancy rates combined.
My advice
- If the project already has a base image, I recommend keeping to use it.
- If the project is a new project, I recommend to consider the
alpine
base image. It’s can make the images extremely small. - If you are using compiled languages, like Golang, just use
alpine
, It uses the least number of dependencies(Some images even usescratch
as their base. This images is for the tool used by every one, so it’s need to control the size) - If you are using Python, just use
stretch
andjessie
, not need to useslim
. Sometimes, you may even need to install some dependencies using gcc. The size of the whole images will inevitably expand
Personally, I’d like to use Debian
as the base image in production:
- The disk is very large in modern server
- The layer you need to pull in every round update is small, and you may not worry about the base image size
- Using Debian could be more stable
Reference
Docker hub Python image Docker hub Golang image Docker hub Node image