“This post is about how to build, light-weight, highly optimized Docker images with the help of a multi-stage image build technique, to get the most out of this article it’s expected for the viewer to have a **basic understanding of Docker. if not, you could follow along and the rest would be a history”
Vada Pav is the most beloved Indian street food, which doesn’t burn a hole in your pocket and it’s a perfect combination of Spicy, Sweet, Salty all at the same time, A soft cushiony pav, stuffed with a golden-fried spiced (potato) patty, covered with coriander chutneys and a sprinkling of garlicky masala — the vada pav is food heaven, an instant energy booster, rightly termed as disco-in-your-mouth.
Pav (an Indian round dumpling bread)
Salt & Spice
Get a Potato + Wash + Peal + Boil + Smash > Make Round dumpling of the potato.
Cover it with Batter (White Flour + Water) and Deep Fry the round dumpling of the potato in Oil
Put Potato dumplings in the bread granish it with green chillies & sliced onions
This is what a really bad, ugly looking Vada pav looks like, which contains most of the unrequired things which were used while creating the Vada Pav itself, that includes oil spill on top of it, with white flour on the vada pav which was used to make the batter as well as left-over potato peels. well, that’s a complete disaster.
What really made the difference between both of them, were that the bad vada pav contains most of the stuff which was used to make it vada pav, which isn’t even needed while serving finally to Mr. Gordon Ramsay.
Wait, How is Vada Pav, Related to that of Dockerfile?
“It’s a technique which involves removing all the unrequired tools, dependencies needed to build an application and only contain the final application build which helps to create lightweight Docker Images.”
The Multi-Stage Docker build feature is available from Docker 17.05 onwards. Using Multi-Stage build is a good practice as it encourages to keep only the things which are needed in the final Docker image and leaving behind all the build-dependencies and other files that are not needed. This involves breaking the Dockerfile into two or more stages, Build stages contains tools needed for building the application inside of the DockerImage and then the Second stage will only contain the final build artifacts being copied from the first layer to be executed.
In order to get a better understanding of how Multi-Stage Docker builds works to let’s understand the same with the help of an example. In this case, we would be Dockerizing a NODE.JS application and separate it into two stages and Build and Execution stages.
In the above example, we could see that there are two stages, the first stage copies files required to build the application after which the dependencies are installed and finally the application is built. the build application is then copied into a final stage which contains instructions to execute the application.
The Final image copies the contents from the build stage using the COPY instruction and specifies the reference of the image from which the contents can be copied, this really helps in only containing the final application which is needed rather than the build dependencies, and tools which are not needed when the application is used by the end-user. Thereby it improves the overall performance & size of the Docker image which is built.
So if compare the Analogy of Dockerfile with that of Vada Pav, the both mean the same thing is to remove unrequired Build Dependencies
What is really common between a good Vada Pav and a Docker Image is that we need to avoid any build level dependencies that are present while finally serving it to end-user. In order to further understand the best practises that can be applied while Building Docker images here’s a curated list 20+ of tips and tricks that can be followed.