A comparision between multistage build and singlestage build in Docker
Last updated
Last updated
Image size is one of the essential considerations while build a production grade docker image. Container images often include development tools, libraries and other necessary application dependencies. These tools and dependencies can drastically increase the image size.
In this blog, we will see a size comparision between a single stage docker image and a multi stage docker image of a simple hello-world golang application.
Before build the image, Lets build a simple golang application.
Using the above dockerfile code, we can create a docker image of the golang application with only a single stage. Lets check the size of the image.
Did you notice 247MB? That too for just a hello world application. Isn't it way too heavy. Multistage builds are here to rescue us.
Using the above dockerfile code, we can create a docker image of the golang application with multi stage builds.
###1.85MB docker image of the same hello world application.
74.89% lighter images.
We're constructing the image through a two-stage process. Initially, we employ a Golang base image, embed our code within it, and compile our executable file, App. Moving on to the next stage, we utilize a fresh Alpine base image, transferring the binary created in the previous step to this new stage.
This is just a small example. Imagine this for a large production grade application. Multistage builds are magical, if you haven’t explored it already – go ahead and do it.