Multi Stage Docker Build To Reduce Image Size Up to 800 Percent:

Ghazanfar Ali
4 min readOct 19, 2023

--

Introduction:

In this project we will understand the concept of multi stage docker build and second thing we will cover is distroless docker images. By using distroless images we can get maximum benefit from multi stage docker file. Let suppose you are building a java base application through docker file using ubuntu base image to build that image you may require hundred of dependencies but to execute your java application you only require JRE and java binary but if your using dimple docker file it and using the same ubuntu machine that comes up with alot of over load that are even not require to during execution of your application, so here comes the multi stage docker file concept in which we split our docker file in multiple parts. In stage one we can use base image and use run command to install all the dependency packages and use the second stage for execution with CMD or ENTRYPOINT commands in docker file. In stage 2 we will not use ubuntu again but very minimal image that require to execute our application or we can also use distroless image here in stage 2.

Our docker multistage file can have end number of stages but we will have a final stage which will have a minimalistic image for instance in first stage we use the heavy image like ubuntu for build then your backend we create a new stage at the end we created a final stage with minimal image like jre 11, and copy the build file from stage 1 and execute in this stage through CMD or ENTRYPOINT.

Distroless Images:

Now we will understand what are docker distroless images, distroless image is basically a very minimal image that will hardly have any packages for e.g we choose python distroless image it will only have python run time environment. These images also provide highest level of security because no unnecessary packages are present in the image which can create bug or vulnerability.

Now we will do some hands on docker multi stage build file and simple docker file without stages and analyze the difference in both, for this example we will take golang docker file,

First clone the repo in your local system:

Its a go based simple calculator:

First we will containerized this application without multi stage docker file:

We will build image from this file:

It created an image of size around 900 MB, even if we do not use ubuntu image and directly use golang image then the image size will be around 400MB:

Now we will create docker image form multi stage docker file, in stage 1 is only dedicated to build step and created the binary and in the final stage we are executing that binary and using the special image called scratch which is minimal distroless docker image:

Now check the image size which is just 1.83 MB of same application we have almost reduced the image size by 800 percent:

This image is not only reduced in size but also has high security because it do not contain unnecessary packages in this image.

That’s all in this project!

--

--

Ghazanfar Ali

I write technical blogs on DevOps and AWS, Azure and GCP