This content originally appeared on Level Up Coding - Medium and was authored by Pavan Kumar
Reduce the size of the Docker image using a Multi-Stage build
Writing Dockerfile seems easy until and unless you end up writing docker images having a size of 0.5GB. Yes, that’s true. I was recently working on a React JS Project, where I was building a front-end application. There was nothing new about the React JS Project. I was using the same package.json, the same set of dependencies that a typical frontend app would be using. Now, I decided to move the application to Kubernetes. So I pushed the docker image to DockerHub and tried to deploy a Pod.
I was using a local cluster and the pod took 5mins7sec to start. I was awestruck. And then I deployed it to the AKS Cluster ( Azure Kubernetes Cluster ). Surprisingly it took 3mins40sec. That was huge. I tried stressing the pod to trigger the Kubernetes HPA. The new pod takes the same 3mins to come up and eventually the application gets overwhelmed with too many requests. Upon Investigation I figured out that the size of the image is the bottleneck, Of course, you don't want to have a docker image of such a huge size ( O.5GB ). And that is when I realized that I will have to do something about the docker image size. And hence I utilized the concept of Docker multi-stage build.
What is the entire story all about? (TLDR)
- Reducing the size of Docker Images using Multi-Stage build.
- Understand the Concept of Multistage build.
Story Resources
- GitHub Link: https://github.com/pavan-kumar-99/medium-manifests
- GitHub Branch: multistage-build-docker
Docker Build:
Here was my Initial Docker Image for a typical React JS Application. If you see line1, I was using the alpine version of the node. After building the Dockerfile, here is the size of it.

Well, this is the typical way of building Images and it seems easy to write such docker files. But here are the disadvantages of using such an approach from the Kubernetes perspective.
a) The size of the image directly affects the Pod startup time.
b) The lesser the size of the image the faster the pod starts.
Docker Multi-Stage Build:
Multistage builds feature in Dockerfiles enables you to create smaller container images with better caching and a smaller security footprint. With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don’t want in the final image.
Seems different? Alright, let us understand the file.
The first line is called as a stage. The stages are not named, and you refer to them by their integer number, starting with 0 for the first FROM instruction. However, you can name your stages, by adding an AS <NAME> to the FROM instruction.
And then the rest of the steps remain the same. One step that is the game changer here is the COPY — from ( Line 18 ). With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don’t want in the final image.
Let us build the image now.

Wow, Yes!! That is true. The image size got reduced by 95%. The Image size is now 27.2 MB.

My local cluster took 25 seconds to pull the image and to get started. And my AKS Cluster took 10seconds to pull the image and get started.
Isn’t this awesome :) !!
Well, that is how you utilize multi stage build in docker to reduce the size of your docker images. Please feel free to share your experience while working on these policies in the comment section.
Until next time…..
Recommended
- Policies as Code in Kubernetes using jsPolicy
- MlOps: Machine learning Pipelines using kubeflow
- HashicVault Secrets in Kubernetes with CSI Driver
- Terraforming the GitOps Way !!!
How I reduced the size of my Docker Image by 95% was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Pavan Kumar

Pavan Kumar | Sciencx (2022-10-06T14:52:05+00:00) How I reduced the size of my Docker Image by 95%. Retrieved from https://www.scien.cx/2022/10/06/how-i-reduced-the-size-of-my-docker-image-by-95/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.