What is an Multistage Dockerfile? (original) (raw)

Last Updated : 8 Sep, 2025

A multistage Dockerfile is a feature introduced in Docker to address the challenge of creating lean and efficient container images Traditionally, Docker images used to contain all the dependencies, libraries, and tools required to run an application, leading to bloated images that consume unnecessary disk space and hence increase the deployment times

Now Multistage builds allow developers to build multiple intermediate images within a single Dockerfile, and each intermediate image serves a specific purpose in the build process.

Dockeroptimzation

Image Optimization

Multistage Dockerfile Working

In a multistage Dockerfile, developers define multiple build stages, each encapsulating a specific set of instructions and dependencies. These stages can be named and referenced within the Dockerfile, enabling seamless communication between them.

reducedimagedocker

Use Multi-Stage Builds

Code template to get you started For Multistage Dockerfile

Build stage with development tools

FROM maven:3.5-jdk-8 as build WORKDIR /app COPY . . RUN mvn clean package

#FInal Stage FROM tomcat:8.0.20-jre8 COPY --from=build /app/target/maven-web-app*.war /usr/local/tomcat/webapps/maven-web-application.war

Explanation Of The Code

**Build Stage (build):

**Final Stage:

Docker-build

final-stage

file

Name Your Build Stages

The AS keyword in the FROM instruction lets us to assign names to the construction phases. In spite of keeping everything clear, this naming makes sure that directives like COPY remain intact if the Dockerfile is later reorganized. Kindly refer to the command below for your reference.

FROM maven:3.5-jdk-8 as build

Stop At A Specific Build Stage

When getting the Docker image, we may provide the target build stage with the --target flag to stop at that particular point. This allows you to halt the build process as certain points without building the stages that followed.

docker build --target build -t your-image-name .

Distroless Images in Multi-Stage Dockerfiles

Distroless images are lightweight container images provided by Google that include only the application runtime dependencies**without a package manager, shell, or OS utilities. This makes them significantly smaller and more secure than traditional base images like Ubuntu or Debian.

In the context of multi-stage Dockerfiles:

Use An External Image As A Stage

The FROM instruction is employed to reference an image from a Docker registry or repository when employing an external image as a stage in a Dockerfile. You can reuse pre-built images as stages in your multi-stage builds through this approach.

Build stage with development tools

FROM maven:3.5-jdk-8 as build WORKDIR /app COPY . . RUN mvn clean package #FInal Stage FROM tomcat:8.0.20-jre8 COPY --from=build /app/target/maven-web-app*.war /usr/local/tomcat/webapps/maven-web-application.war

Differences Between Legacy Builder And BuildKit

Necessary Commands Required For Multi-Stage Dockerfile

Realtime Use Case Examples

Stream Processing And Analytics

An application ingesting and analyzing data streams like tweets or stock prices in real-time To handle this efficiently, we require leverage multi-stage builds:

Best Practices For Multistage Dockerfile