DEV Community

Cover image for Advanced Optimization of Docker Images Using Multi-Stage Builds
Arun Kumar
Arun Kumar

Posted on

1

Advanced Optimization of Docker Images Using Multi-Stage Builds

🚨 The Problem with Single-Stage Builds

  • The final image contains unnecessary dependencies (e.g., compilers, libraries, build tools).
  • This makes the image large, slow to deploy, and less secure.

βœ… The Solution: Multi-Stage Builds

  1. First stage (Build Stage): Installs dependencies, compiles the app.
  2. Second stage (Runtime Stage): Copies only the necessary files, keeping the image small and secure.

🐍 Multi-Stage Build for a Python FastAPI App

This example optimizes a Python FastAPI application.

# 🟒 Stage 1: Build Environment (Full Python + Dependencies)
FROM python:3.10 AS builder

WORKDIR /app

# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# 🟒 Stage 2: Production Image (Minimal Python)
FROM python:3.10-slim AS runner

WORKDIR /app

# Copy only necessary files from the builder stage
COPY --from=builder /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages
COPY . .

# Set environment variables
ENV PORT=5000

# Expose port and run the application
EXPOSE 5000
CMD ["python", "app.py"]
Enter fullscreen mode Exit fullscreen mode

πŸš€ Why is This Better?

βœ… Smaller Image: Only includes necessary runtime files.
βœ… Faster Build: Dependencies are installed only once in the builder stage.
βœ… More Secure: The final image does not contain compilers or extra tools.


β˜• Multi-Stage Build for a Java Spring Boot App

Since Java requires a JDK for compilation but only needs a JRE for runtime, a multi-stage build is ideal.

# 🟒 Stage 1: Build Application (Using JDK)
FROM maven:3.8.6-openjdk-17 AS builder

WORKDIR /app

# Copy source code and build the JAR file
COPY pom.xml .
COPY src ./src
RUN mvn clean package -DskipTests

# 🟒 Stage 2: Production Image (Using JRE)
FROM openjdk:17-jdk-slim AS runner

WORKDIR /app

# Copy only the compiled JAR file
COPY --from=builder /app/target/myapp.jar myapp.jar

# Run the application
EXPOSE 8080
CMD ["java", "-jar", "myapp.jar"]
Enter fullscreen mode Exit fullscreen mode

πŸš€ Why is This Better?

βœ… Smaller Image: Uses jdk-slim, avoiding unnecessary files.
βœ… Faster Startup: No need for Maven in production.
βœ… Better Performance: Minimal runtime environment.


πŸ“Š Single-Stage vs. Multi-Stage Builds: A Quick Comparison

Feature Single-Stage Build Multi-Stage Build
Image Size Large (includes build tools) Small (only runtime files)
Security More vulnerable (contains unnecessary tools) More secure (minimal runtime)
Performance Slower startup Faster startup
Best For Dev/test environments Production deployments

βœ… When Should You Use Multi-Stage Builds?

  • If your app has build-time dependencies (e.g., Maven, Node.js, Python libraries).
  • If you want a smaller and more secure image.
  • If you don’t need build tools in the final image.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay