{"id":14076,"date":"2022-10-03T14:21:06","date_gmt":"2022-10-03T14:21:06","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=14076"},"modified":"2025-07-12T05:40:16","modified_gmt":"2025-07-12T05:40:16","slug":"how-to-optimize-faster-builds-and-smaller-docker-images-using-multistaging-build","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-optimize-faster-builds-and-smaller-docker-images-using-multistaging-build\/","title":{"rendered":"Docker Tutorials: Image &#8211; Optimize faster builds and smaller docker images using multistaging build?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Problem with traditional approach of creating docker image?<\/h2>\n\n\n\n<p>One of the most challenging things about building images is keeping the image size down. Each instruction in the Dockerfile adds a layer to the image, and you need to remember to clean up any artifacts you don\u2019t need before moving on to the next layer.<\/p>\n\n\n\n<p>It was actually very common to have one Dockerfile to use for development (which contained everything needed to build your application), and a slimmed-down one to use for production, which only contained your application and exactly what was needed to run it. This has been referred to as the \u201cbuilder pattern\u201d. Maintaining two Dockerfiles is not ideal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Docker multistaging build?<\/h2>\n\n\n\n<p>Multi-stage builds are a method of organizing a Dockerfile to minimize the size of the final container, improve run time performance, allow for better organization of Docker commands and files, and provide a standardized method of running build actions. A multi-stage build is done by creating different sections of a Dockerfile, each referencing a different base image. This allows a multi-stage build to fulfill a function previously filled by using multiple docker files, copying files between containers, or running different pipelines.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"371\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/05\/docke-multistaging-build2-1024x371.png\" alt=\"\" class=\"wp-image-14079\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/05\/docke-multistaging-build2-1024x371.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/05\/docke-multistaging-build2-300x109.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/05\/docke-multistaging-build2-768x278.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/05\/docke-multistaging-build2.png 1049w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Before multi-stage builds<\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/f4418a67d3a1dc37fd8088d2299f9af4.js\"><\/script>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"797\" height=\"322\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/05\/docke-multistaging-build3.png\" alt=\"\" class=\"wp-image-14080\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/05\/docke-multistaging-build3.png 797w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/05\/docke-multistaging-build3-300x121.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/05\/docke-multistaging-build3-768x310.png 768w\" sizes=\"auto, (max-width: 797px) 100vw, 797px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">After Use multi-stage builds<\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/0be2f4fd3aea361c2502a9df0cbb0fd0.js\"><\/script>\n\n\n\n<h4 class=\"wp-block-heading\">You only need the single Dockerfile. You don\u2019t need a separate build script, either. Just run docker build.<\/h4>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/4ce521fe759a412583880df1d78ef8e5.js\"><\/script>\n\n\n\n<p>Docker Tutorials Fundamental To Advanced-2021 Crash Course:- <a href=\"https:\/\/bit.ly\/3hOIbTB\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/bit.ly\/3hOIbTB<\/a><\/p>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_95230\"><iframe loading=\"lazy\"  id=\"_ytid_27645\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_95230\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Problem with traditional approach of creating docker image? One of the most challenging things about building images is keeping the image size down. Each instruction in the Dockerfile adds a&#8230; <\/p>\n","protected":false},"author":1,"featured_media":14078,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[4862,1],"tags":[],"class_list":["post-14076","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-docker","category-sql"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/14076","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=14076"}],"version-history":[{"count":6,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/14076\/revisions"}],"predecessor-version":[{"id":31438,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/14076\/revisions\/31438"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/14078"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=14076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=14076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=14076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}