{"id":10213,"date":"2025-05-12T14:49:38","date_gmt":"2025-05-12T14:49:38","guid":{"rendered":"https:\/\/www.magetop.com\/blog\/?p=10213"},"modified":"2025-05-12T14:51:09","modified_gmt":"2025-05-12T14:51:09","slug":"multi-stage-builds-in-docker","status":"publish","type":"post","link":"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/","title":{"rendered":"Multi-Stage Builds In Docker: Save Image Size And Improve Security"},"content":{"rendered":"\n<p>Docker multi-stage builds help developers create smaller, cleaner, and more secure images by separating the build environment from the final runtime. In this post, you\u2019ll learn what multi-stage builds in Docker are and how to use them effectively.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Multi-Stage Builds in Docker?<\/h2>\n\n\n\n<p>Normally, when you build an image that compiles code (e.g., with <code>gcc<\/code>, <code>npm<\/code>, or <code>go build<\/code>), those tools and dependencies stay in the final image \u2014 even if they\u2019re no longer needed at runtime.<\/p>\n\n\n\n<p>This leads to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\udeab Unnecessary bloat<\/li>\n\n\n\n<li>\ud83d\udee1\ufe0f Higher security risks<\/li>\n\n\n\n<li>\ud83d\udc0c Slower deploys<\/li>\n<\/ul>\n\n\n\n<p>Multi-stage builds solve this by using multiple <code>FROM<\/code> statements in a single Dockerfile \u2014 copying only the <strong>final output<\/strong> from the build phase into a clean runtime environment.<\/p>\n\n\n\n<p><strong>Example: Python App Using Poetry for Dependency Management<\/strong><\/p>\n\n\n\n<p>Let\u2019s say you have a simple Python app that uses Poetry for dependency management.<\/p>\n\n\n\n<p>Create a file named <code>pyproject.toml<\/code>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;tool.poetry]\nname = &quot;hello-docker&quot;\nversion = &quot;0.1.0&quot;\ndescription = &quot;&quot;\nauthors = &#x5B;&quot;You&quot;]\n\n&#x5B;tool.poetry.dependencies]\npython = &quot;^3.11&quot;\n\n&#x5B;tool.poetry.scripts]\nhello = &quot;app:main&quot;\n<\/pre><\/div>\n\n\n<p>Next, create <code>app.py<\/code>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndef main():\n    print(&quot;Hello from a Python multi-stage Docker build!&quot;)\n\nif __name__ == &quot;__main__&quot;:\n    main()\n<\/pre><\/div>\n\n\n<p><strong>Dockerfile with Multi-Stage Build<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# Stage 1: Build using Poetry\nFROM python:3.11-slim AS builder\n\nRUN pip install poetry\n\nWORKDIR \/app\n\nCOPY pyproject.toml .\nCOPY app.py .\n\n# Install only dependencies, skip installing current project\nRUN poetry config virtualenvs.create false \\\n    &amp;&amp; poetry install --only main --no-root --no-interaction\n\n# Stage 2: Clean runtime image\nFROM python:3.11-alpine\n\nWORKDIR \/app\n\nCOPY --from=builder \/app \/app\n\nCMD &#x5B;&quot;python&quot;, &quot;app.py&quot;]\n<\/pre><\/div>\n\n\n<p>Build and Run<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndocker build -t python-multistage .\ndocker run --rm python-multistage\n<\/pre><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"465\" data-attachment-id=\"10216\" data-permalink=\"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/screenshot-4\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Screenshot-3.png?fit=1120%2C651&amp;ssl=1\" data-orig-size=\"1120,651\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Screenshot\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Screenshot-3.png?fit=300%2C174&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Screenshot-3.png?fit=800%2C465&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Screenshot-3.png?resize=800%2C465&#038;ssl=1\" alt=\"Multi-Stage Builds In Docker build\" class=\"wp-image-10216\" srcset=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Screenshot-3.png?resize=1024%2C595&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Screenshot-3.png?resize=300%2C174&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Screenshot-3.png?resize=768%2C446&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Screenshot-3.png?w=1120&amp;ssl=1 1120w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure><\/div>\n\n\n<p>Expected output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nHello from a Python multi-stage Docker build!\n<\/pre><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"466\" data-attachment-id=\"10218\" data-permalink=\"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/screenshot_1-18\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Screenshot_1-5.png?fit=1119%2C652&amp;ssl=1\" data-orig-size=\"1119,652\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Screenshot_1\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Screenshot_1-5.png?fit=300%2C175&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Screenshot_1-5.png?fit=800%2C466&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Screenshot_1-5.png?resize=800%2C466&#038;ssl=1\" alt=\"Multi-Stage Builds In Docker run\" class=\"wp-image-10218\" srcset=\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Screenshot_1-5.png?resize=1024%2C597&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Screenshot_1-5.png?resize=300%2C175&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Screenshot_1-5.png?resize=768%2C447&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Screenshot_1-5.png?w=1119&amp;ssl=1 1119w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure><\/div>\n\n\n<p><strong>Size Comparison (Approximate)<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Method<\/strong><\/td><td><strong>Image Size<\/strong><\/td><\/tr><tr><td>Without multi-stage<\/td><td>~300MB<\/td><\/tr><tr><td>With multi-stage<\/td><td>~30MB \ud83e\udde8<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Benefits Recap<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83e\uddfc Clean final images \u2014 no compilers or build tools<\/li>\n\n\n\n<li>\ud83d\udce6 Smaller image sizes = faster CI\/CD and deploys<\/li>\n\n\n\n<li>\ud83d\udd10 Lower attack surface = better security<\/li>\n\n\n\n<li>\ud83d\udc4f Easier to maintain consistent build vs. runtime environments<\/li>\n<\/ul>\n\n\n\n<p><strong>Use Cases<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Compiling code (Go, Node.js, Java, Rust)<\/li>\n\n\n\n<li>Installing &amp; optimizing frontend builds (<code>npm run build<\/code>)<\/li>\n\n\n\n<li>Packaging Python apps with <code>pip<\/code> but leaving out the cache<\/li>\n<\/ul>\n\n\n\n<p>Multi-stage builds are one of Docker\u2019s most powerful and underutilized features. They help you build production-grade containers that are both efficient and secure. Once you try them, you\u2019ll never go back to bulky images.<\/p>\n\n\n\n<p>You can read more useful articles like&nbsp;<strong><a href=\"https:\/\/www.magetop.com\/blog\/how-to-build-custom-docker-images\/\">Dockerfile Explained: How to Build Custom Docker Images<\/a><\/strong>.<\/p>\n\n\n\n<p>Follow us for the more helpful posts!<\/p>\n\n\n\n<p>We hope this is a useful post for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Docker multi-stage builds help developers create smaller, cleaner, and more secure images by separating the build environment from the final<\/p>\n","protected":false},"author":106,"featured_media":10220,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[2306,2318,2319,2317,2320],"class_list":["post-10213","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento-2-tutorials","tag-docker","tag-multi-stage","tag-multi-stage-builds","tag-multi-stage-builds-in-docker","tag-multi-stage-builds-in-docker-save-image-size-and-improve-security"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Multi-Stage Builds In Docker: Save Image Size And Improve Security - Magetop Blog<\/title>\n<meta name=\"description\" content=\"Docker multi-stage builds help developers create smaller, cleaner, and more secure images by separating the build environment from the final runtime. In this post, you\u2019ll learn what multi-stage builds in Docker are and how to use them effectively.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Multi-Stage Builds In Docker: Save Image Size And Improve Security - Magetop Blog\" \/>\n<meta property=\"og:description\" content=\"Docker multi-stage builds help developers create smaller, cleaner, and more secure images by separating the build environment from the final runtime. In this post, you\u2019ll learn what multi-stage builds in Docker are and how to use them effectively.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/\" \/>\n<meta property=\"og:site_name\" content=\"Magetop Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/MagetopStore\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-12T14:49:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-12T14:51:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Multi-Stage-Builds-In-Docker-Save-Image-Size-And-Improve-Security.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1355\" \/>\n\t<meta property=\"og:image:height\" content=\"753\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Aaron LX\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@MagetopStore\" \/>\n<meta name=\"twitter:site\" content=\"@MagetopStore\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Aaron LX\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/\"},\"author\":{\"name\":\"Aaron LX\",\"@id\":\"https:\/\/www.magetop.com\/blog\/#\/schema\/person\/b8770690a02cc53a273d6b7205229ff7\"},\"headline\":\"Multi-Stage Builds In Docker: Save Image Size And Improve Security\",\"datePublished\":\"2025-05-12T14:49:38+00:00\",\"dateModified\":\"2025-05-12T14:51:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/\"},\"wordCount\":294,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.magetop.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Multi-Stage-Builds-In-Docker-Save-Image-Size-And-Improve-Security.png?fit=1355%2C753&ssl=1\",\"keywords\":[\"Docker\",\"Multi-Stage\",\"Multi-Stage Builds\",\"Multi-Stage Builds In Docker\",\"Multi-Stage Builds In Docker: Save Image Size And Improve Security\"],\"articleSection\":[\"Magento 2 Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/\",\"url\":\"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/\",\"name\":\"Multi-Stage Builds In Docker: Save Image Size And Improve Security - Magetop Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.magetop.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Multi-Stage-Builds-In-Docker-Save-Image-Size-And-Improve-Security.png?fit=1355%2C753&ssl=1\",\"datePublished\":\"2025-05-12T14:49:38+00:00\",\"dateModified\":\"2025-05-12T14:51:09+00:00\",\"description\":\"Docker multi-stage builds help developers create smaller, cleaner, and more secure images by separating the build environment from the final runtime. In this post, you\u2019ll learn what multi-stage builds in Docker are and how to use them effectively.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Multi-Stage-Builds-In-Docker-Save-Image-Size-And-Improve-Security.png?fit=1355%2C753&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Multi-Stage-Builds-In-Docker-Save-Image-Size-And-Improve-Security.png?fit=1355%2C753&ssl=1\",\"width\":1355,\"height\":753,\"caption\":\"Multi-Stage Builds In Docker: Save Image Size And Improve Security\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.magetop.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Multi-Stage Builds In Docker: Save Image Size And Improve Security\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.magetop.com\/blog\/#website\",\"url\":\"https:\/\/www.magetop.com\/blog\/\",\"name\":\"Magetop Blog\",\"description\":\"Exploring Magento Tips, Tricks, and Trends\",\"publisher\":{\"@id\":\"https:\/\/www.magetop.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.magetop.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.magetop.com\/blog\/#organization\",\"name\":\"Magetop.com\",\"url\":\"https:\/\/www.magetop.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.magetop.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2021\/11\/logo.png?fit=475%2C475&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2021\/11\/logo.png?fit=475%2C475&ssl=1\",\"width\":475,\"height\":475,\"caption\":\"Magetop.com\"},\"image\":{\"@id\":\"https:\/\/www.magetop.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/MagetopStore\",\"https:\/\/x.com\/MagetopStore\",\"https:\/\/www.linkedin.com\/company\/magetop\",\"https:\/\/www.pinterest.com\/magetop\",\"https:\/\/www.youtube.com\/channel\/UCXoiJsz88OfPmwa8QpUkwOA\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.magetop.com\/blog\/#\/schema\/person\/b8770690a02cc53a273d6b7205229ff7\",\"name\":\"Aaron LX\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.magetop.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/11\/475315059_122137709240563546_260104055231757176_n.jpg?fit=96%2C96&#038;ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/11\/475315059_122137709240563546_260104055231757176_n.jpg?fit=96%2C96&#038;ssl=1\",\"caption\":\"Aaron LX\"},\"description\":\"Aaron is a passionate writer, crazy about shopping, eCommerce and trends. Besides his outstanding research skills and a positive mind, Aaron eagerly shares his experience with the readers.\",\"url\":\"https:\/\/www.magetop.com\/blog\/author\/aaron-lx\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Multi-Stage Builds In Docker: Save Image Size And Improve Security - Magetop Blog","description":"Docker multi-stage builds help developers create smaller, cleaner, and more secure images by separating the build environment from the final runtime. In this post, you\u2019ll learn what multi-stage builds in Docker are and how to use them effectively.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/","og_locale":"en_US","og_type":"article","og_title":"Multi-Stage Builds In Docker: Save Image Size And Improve Security - Magetop Blog","og_description":"Docker multi-stage builds help developers create smaller, cleaner, and more secure images by separating the build environment from the final runtime. In this post, you\u2019ll learn what multi-stage builds in Docker are and how to use them effectively.","og_url":"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/","og_site_name":"Magetop Blog","article_publisher":"https:\/\/www.facebook.com\/MagetopStore","article_published_time":"2025-05-12T14:49:38+00:00","article_modified_time":"2025-05-12T14:51:09+00:00","og_image":[{"width":1355,"height":753,"url":"https:\/\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Multi-Stage-Builds-In-Docker-Save-Image-Size-And-Improve-Security.png","type":"image\/png"}],"author":"Aaron LX","twitter_card":"summary_large_image","twitter_creator":"@MagetopStore","twitter_site":"@MagetopStore","twitter_misc":{"Written by":"Aaron LX","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/#article","isPartOf":{"@id":"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/"},"author":{"name":"Aaron LX","@id":"https:\/\/www.magetop.com\/blog\/#\/schema\/person\/b8770690a02cc53a273d6b7205229ff7"},"headline":"Multi-Stage Builds In Docker: Save Image Size And Improve Security","datePublished":"2025-05-12T14:49:38+00:00","dateModified":"2025-05-12T14:51:09+00:00","mainEntityOfPage":{"@id":"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/"},"wordCount":294,"commentCount":0,"publisher":{"@id":"https:\/\/www.magetop.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Multi-Stage-Builds-In-Docker-Save-Image-Size-And-Improve-Security.png?fit=1355%2C753&ssl=1","keywords":["Docker","Multi-Stage","Multi-Stage Builds","Multi-Stage Builds In Docker","Multi-Stage Builds In Docker: Save Image Size And Improve Security"],"articleSection":["Magento 2 Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/","url":"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/","name":"Multi-Stage Builds In Docker: Save Image Size And Improve Security - Magetop Blog","isPartOf":{"@id":"https:\/\/www.magetop.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/#primaryimage"},"image":{"@id":"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Multi-Stage-Builds-In-Docker-Save-Image-Size-And-Improve-Security.png?fit=1355%2C753&ssl=1","datePublished":"2025-05-12T14:49:38+00:00","dateModified":"2025-05-12T14:51:09+00:00","description":"Docker multi-stage builds help developers create smaller, cleaner, and more secure images by separating the build environment from the final runtime. In this post, you\u2019ll learn what multi-stage builds in Docker are and how to use them effectively.","breadcrumb":{"@id":"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/#primaryimage","url":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Multi-Stage-Builds-In-Docker-Save-Image-Size-And-Improve-Security.png?fit=1355%2C753&ssl=1","contentUrl":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Multi-Stage-Builds-In-Docker-Save-Image-Size-And-Improve-Security.png?fit=1355%2C753&ssl=1","width":1355,"height":753,"caption":"Multi-Stage Builds In Docker: Save Image Size And Improve Security"},{"@type":"BreadcrumbList","@id":"https:\/\/www.magetop.com\/blog\/multi-stage-builds-in-docker\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.magetop.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Multi-Stage Builds In Docker: Save Image Size And Improve Security"}]},{"@type":"WebSite","@id":"https:\/\/www.magetop.com\/blog\/#website","url":"https:\/\/www.magetop.com\/blog\/","name":"Magetop Blog","description":"Exploring Magento Tips, Tricks, and Trends","publisher":{"@id":"https:\/\/www.magetop.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.magetop.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.magetop.com\/blog\/#organization","name":"Magetop.com","url":"https:\/\/www.magetop.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.magetop.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2021\/11\/logo.png?fit=475%2C475&ssl=1","contentUrl":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2021\/11\/logo.png?fit=475%2C475&ssl=1","width":475,"height":475,"caption":"Magetop.com"},"image":{"@id":"https:\/\/www.magetop.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/MagetopStore","https:\/\/x.com\/MagetopStore","https:\/\/www.linkedin.com\/company\/magetop","https:\/\/www.pinterest.com\/magetop","https:\/\/www.youtube.com\/channel\/UCXoiJsz88OfPmwa8QpUkwOA"]},{"@type":"Person","@id":"https:\/\/www.magetop.com\/blog\/#\/schema\/person\/b8770690a02cc53a273d6b7205229ff7","name":"Aaron LX","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.magetop.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/11\/475315059_122137709240563546_260104055231757176_n.jpg?fit=96%2C96&#038;ssl=1","contentUrl":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/11\/475315059_122137709240563546_260104055231757176_n.jpg?fit=96%2C96&#038;ssl=1","caption":"Aaron LX"},"description":"Aaron is a passionate writer, crazy about shopping, eCommerce and trends. Besides his outstanding research skills and a positive mind, Aaron eagerly shares his experience with the readers.","url":"https:\/\/www.magetop.com\/blog\/author\/aaron-lx\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Multi-Stage-Builds-In-Docker-Save-Image-Size-And-Improve-Security.png?fit=1355%2C753&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/paOdw5-2EJ","jetpack-related-posts":[{"id":10237,"url":"https:\/\/www.magetop.com\/blog\/how-to-use-docker-compose-multi-container-applications\/","url_meta":{"origin":10213,"position":0},"title":"How To Use Docker Compose: Multi-Container Applications","author":"Aaron LX","date":"May 14, 2025","format":false,"excerpt":"Managing multi-container applications manually with Docker commands can quickly become complex and error-prone. Docker Compose simplifies this process using a single configuration file to define and run multiple containers effortlessly. In this guide, you'll learn how to use docker compose through a real-world example. What is Docker Compose? Docker Compose\u2026","rel":"","context":"In &quot;Magento 2 Tutorials&quot;","block_context":{"text":"Magento 2 Tutorials","link":"https:\/\/www.magetop.com\/blog\/magento-2-tutorials\/"},"img":{"alt_text":"How To Use Docker Compose Multi-Container Applications","src":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/How-To-Use-Docker-Compose-Multi-Container-Applications.png?fit=1200%2C667&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/How-To-Use-Docker-Compose-Multi-Container-Applications.png?fit=1200%2C667&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/How-To-Use-Docker-Compose-Multi-Container-Applications.png?fit=1200%2C667&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/How-To-Use-Docker-Compose-Multi-Container-Applications.png?fit=1200%2C667&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/How-To-Use-Docker-Compose-Multi-Container-Applications.png?fit=1200%2C667&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":10287,"url":"https:\/\/www.magetop.com\/blog\/docker-security-best-practices-for-developers\/","url_meta":{"origin":10213,"position":1},"title":"Docker Security Best Practices For Developers","author":"Aaron LX","date":"May 26, 2025","format":false,"excerpt":"Containerization makes deployment faster and more consistent\u2014but security should never be an afterthought. In this guide, we\u2019ll explore essential Docker security best practices for developers, with a practical Python example to show how small changes can protect your app. Why Docker Security Matters Docker simplifies packaging applications and their dependencies.\u2026","rel":"","context":"In &quot;Magento 2 Tutorials&quot;","block_context":{"text":"Magento 2 Tutorials","link":"https:\/\/www.magetop.com\/blog\/magento-2-tutorials\/"},"img":{"alt_text":"Docker Security Best Practices For Developers","src":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Docker-Security-Best-Practices-For-Developers.png?fit=1200%2C667&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Docker-Security-Best-Practices-For-Developers.png?fit=1200%2C667&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Docker-Security-Best-Practices-For-Developers.png?fit=1200%2C667&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Docker-Security-Best-Practices-For-Developers.png?fit=1200%2C667&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Docker-Security-Best-Practices-For-Developers.png?fit=1200%2C667&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":10278,"url":"https:\/\/www.magetop.com\/blog\/docker-networking-basics-bridge-host-and-overlay-networks\/","url_meta":{"origin":10213,"position":2},"title":"Docker Networking Basics: Bridge, Host, and Overlay Networks","author":"Aaron LX","date":"May 24, 2025","format":false,"excerpt":"Networking in Docker can be confusing at first, but it's critical for connecting containers to each other and the outside world. In this guide, you\u2019ll learn the basics of bridge, host, and overlay networks\u2014with simple, Python-based examples. What Is Docker Networking? Docker provides built-in networking drivers to allow containers to\u2026","rel":"","context":"In &quot;Magento 2 Tutorials&quot;","block_context":{"text":"Magento 2 Tutorials","link":"https:\/\/www.magetop.com\/blog\/magento-2-tutorials\/"},"img":{"alt_text":"Docker Networking Basics Bridge, Host, and Overlay Networks","src":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Docker-Networking-Basics-Bridge-Host-and-Overlay-Networks.png?fit=1200%2C667&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Docker-Networking-Basics-Bridge-Host-and-Overlay-Networks.png?fit=1200%2C667&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Docker-Networking-Basics-Bridge-Host-and-Overlay-Networks.png?fit=1200%2C667&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Docker-Networking-Basics-Bridge-Host-and-Overlay-Networks.png?fit=1200%2C667&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Docker-Networking-Basics-Bridge-Host-and-Overlay-Networks.png?fit=1200%2C667&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":10251,"url":"https:\/\/www.magetop.com\/blog\/top-10-docker-commands-every-developer-should-know\/","url_meta":{"origin":10213,"position":3},"title":"Top 10 Docker Commands Every Developer Should Know","author":"Aaron LX","date":"May 17, 2025","format":false,"excerpt":"Getting started with Docker can feel intimidating at first, but mastering just a few essential commands will quickly make you feel at home. In this post, you'll learn the top 10 Docker commands that every developer should know \u2014 along with real-world examples and tips on how to explore Docker\u2026","rel":"","context":"In &quot;Magento 2 Tutorials&quot;","block_context":{"text":"Magento 2 Tutorials","link":"https:\/\/www.magetop.com\/blog\/magento-2-tutorials\/"},"img":{"alt_text":"Top 10 Docker Commands Every Developer Should Know","src":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Top-10-Docker-Commands-Every-Developer-Should-Know.png?fit=1200%2C667&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Top-10-Docker-Commands-Every-Developer-Should-Know.png?fit=1200%2C667&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Top-10-Docker-Commands-Every-Developer-Should-Know.png?fit=1200%2C667&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Top-10-Docker-Commands-Every-Developer-Should-Know.png?fit=1200%2C667&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/05\/Top-10-Docker-Commands-Every-Developer-Should-Know.png?fit=1200%2C667&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":10475,"url":"https:\/\/www.magetop.com\/blog\/how-to-fix-docker-build-cache-not-updating\/","url_meta":{"origin":10213,"position":4},"title":"How To Fix Docker Build Cache Not Updating","author":"Aaron LX","date":"July 20, 2025","format":false,"excerpt":"Docker\u2019s build cache is powerful for speeding up builds, but sometimes it gets in the way \u2014 especially when your changes don\u2019t seem to apply. In this guide, you\u2019ll learn why the cache might not update and How To Fix Docker Build Cache Not Updating with a clear example. What\u2026","rel":"","context":"In &quot;Magento 2 Tutorials&quot;","block_context":{"text":"Magento 2 Tutorials","link":"https:\/\/www.magetop.com\/blog\/magento-2-tutorials\/"},"img":{"alt_text":"How To Fix Docker Build Cache Not Updating","src":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/07\/How-To-Fix-Docker-Build-Cache-Not-Updating.png?fit=1200%2C667&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/07\/How-To-Fix-Docker-Build-Cache-Not-Updating.png?fit=1200%2C667&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/07\/How-To-Fix-Docker-Build-Cache-Not-Updating.png?fit=1200%2C667&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/07\/How-To-Fix-Docker-Build-Cache-Not-Updating.png?fit=1200%2C667&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/07\/How-To-Fix-Docker-Build-Cache-Not-Updating.png?fit=1200%2C667&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":10507,"url":"https:\/\/www.magetop.com\/blog\/how-to-fix-no-space-left-on-device-in-docker\/","url_meta":{"origin":10213,"position":5},"title":"How To Fix \u201cNo Space Left On Device\u201d In Docker","author":"Aaron LX","date":"July 23, 2025","format":false,"excerpt":"Running into a no space left on device error while using Docker can be frustrating \u2014 especially when your disk seems fine. This guide explains what causes it, and how to clean up Docker properly with a real example. The Error You might see something like this when building or\u2026","rel":"","context":"In &quot;Magento 2 Tutorials&quot;","block_context":{"text":"Magento 2 Tutorials","link":"https:\/\/www.magetop.com\/blog\/magento-2-tutorials\/"},"img":{"alt_text":"How To Fix \u201cNo Space Left On Device\u201d In Docker","src":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/07\/How-To-Fix-No-Space-Left-On-Device-In-Docker.png?fit=1200%2C667&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/07\/How-To-Fix-No-Space-Left-On-Device-In-Docker.png?fit=1200%2C667&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/07\/How-To-Fix-No-Space-Left-On-Device-In-Docker.png?fit=1200%2C667&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/07\/How-To-Fix-No-Space-Left-On-Device-In-Docker.png?fit=1200%2C667&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.magetop.com\/blog\/wp-content\/uploads\/2025\/07\/How-To-Fix-No-Space-Left-On-Device-In-Docker.png?fit=1200%2C667&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/posts\/10213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/users\/106"}],"replies":[{"embeddable":true,"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/comments?post=10213"}],"version-history":[{"count":5,"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/posts\/10213\/revisions"}],"predecessor-version":[{"id":10221,"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/posts\/10213\/revisions\/10221"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/media\/10220"}],"wp:attachment":[{"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/media?parent=10213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/categories?post=10213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.magetop.com\/blog\/wp-json\/wp\/v2\/tags?post=10213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}