codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐳

Docker

7 / 60 topics
6Dockerfile Basics7Multi-Stage Builds
Tutorials/Docker/Multi-Stage Builds
🐳Docker

Multi-Stage Builds

Updated 2026-05-15
10 min read

Multi-Stage Builds

Introduction

In the world of containerization, optimizing your Docker images is crucial for reducing their size and improving build times. One powerful technique to achieve this is through multi-stage builds. Multi-stage builds allow you to create smaller, more efficient Docker images by separating the build environment from the runtime environment.

This tutorial will guide you through understanding multi-stage builds, how they work, and provide practical examples to help you optimize your Dockerfiles.

Concept

A traditional Dockerfile might include all steps required for building an application, including installing dependencies, compiling code, and running tests. However, these intermediate stages can result in large image sizes because they contain unnecessary tools and files.

Multi-stage builds address this issue by allowing you to use multiple FROM statements in a single Dockerfile. Each FROM statement starts a new stage, and you can copy artifacts from one stage to another. This way, you can keep the final image minimal, containing only what is necessary for running your application.

Examples

Let's walk through some examples to illustrate how multi-stage builds work.

Example 1: Simple Multi-Stage Build

Suppose you have a Node.js application that needs to be built and then run. Here’s how you can use a multi-stage build:

# Stage 1: Build the application
FROM node:14 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

# Stage 2: Run the application
FROM node:14-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
CMD ["node", "server.js"]

Explanation:

  1. Stage 1 (Builder):

    • Uses a full Node.js image (node:14) to install dependencies and build the application.
    • Copies package*.json and runs npm install.
    • Copies the rest of the application code and builds it using npm run build.
  2. Stage 2 (Runner):

    • Uses a smaller, lightweight Node.js image (node:14-alpine) to reduce the final image size.
    • Copies only the built artifacts from the builder stage into this new stage.
    • Sets the command to run the application.

Example 2: Python Application

Here’s another example using a Python application:

# Stage 1: Build the application
FROM python:3.9-slim AS builder
WORKDIR /app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN python setup.py install

# Stage 2: Run the application
FROM python:3.9-alpine
WORKDIR /app
COPY --from=builder /usr/local/lib/python3.9/site-packages ./site-packages
COPY --from=builder /app/app.py ./
CMD ["python", "app.py"]

Explanation:

  1. Stage 1 (Builder):

    • Uses a slim Python image (python:3.9-slim) to install dependencies and build the application.
    • Copies requirements.txt and installs dependencies using pip.
    • Copies the rest of the application code and installs it.
  2. Stage 2 (Runner):

    • Uses a smaller, lightweight Python image (python:3.9-alpine).
    • Copies only the installed packages and the main application file from the builder stage.
    • Sets the command to run the application.

What's Next?

Now that you understand how to use multi-stage builds to optimize your Docker images, the next step is to explore Docker Compose Basics. Docker Compose allows you to define and run multi-container Docker applications easily, making it a powerful tool for managing complex setups.

By combining multi-stage builds with Docker Compose, you can create efficient, scalable, and maintainable containerized environments for your applications.


PreviousDockerfile BasicsNext Docker Compose Basics

Recommended Gear

Dockerfile BasicsDocker Compose Basics