DEV Community

Cover image for Streamlining Production with Docker Multi-Stage Builds
Naveen Malothu
Naveen Malothu

Posted on

Streamlining Production with Docker Multi-Stage Builds

Streamlining Production with Docker Multi-Stage Builds

As a Full Stack Engineer specializing in DevOps, AI Infrastructure, and Cloud, I've come to realize the importance of efficient and lightweight Docker images in production environments. In my experience, Docker multi-stage builds have been a game-changer in reducing image sizes and improving overall deployment times. In this post, I'll share my knowledge on how to leverage Docker multi-stage builds for production, with a focus on practical examples and real-world applications.

Introduction to Multi-Stage Builds

Docker multi-stage builds allow you to separate the build process from the runtime environment, resulting in significantly smaller images. This is achieved by defining multiple FROM instructions in a single Dockerfile, each representing a separate stage. I use this feature extensively in my projects, as it enables me to keep my production images lean and efficient.

Example Use Case: Building a Python Application

Let's consider a simple Python application that uses Flask as the web framework. Our Dockerfile might look like this:

# Stage 1: Build
FROM python:3.9-slim as build
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
RUN python -m flask build

# Stage 2: Runtime
FROM python:3.9-slim
WORKDIR /app
COPY --from=build /app /app
CMD ["flask", "run"]
Enter fullscreen mode Exit fullscreen mode

In this example, we define two stages: build and runtime. The build stage installs the dependencies and builds the application, while the runtime stage copies the built application from the build stage and sets the command to run the application.

Optimizing Image Size

In my experience, one of the biggest advantages of Docker multi-stage builds is the ability to optimize image size. By separating the build process from the runtime environment, we can avoid including unnecessary dependencies in our production images. For instance, if we're building a Java application, we can use a separate stage for compiling the code and then copy the compiled artifacts to the runtime stage.

# Stage 1: Build
FROM maven:3.6-jdk-11 as build
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline
COPY . .
RUN mvn package

# Stage 2: Runtime
FROM openjdk:11-jre
WORKDIR /app
COPY --from=build /app/target /app
CMD ["java", "-jar", "myapp.jar"]
Enter fullscreen mode Exit fullscreen mode

Best Practices and Conclusion

In conclusion, Docker multi-stage builds are a powerful tool for streamlining production environments. I use them extensively in my projects, and I've seen significant improvements in image sizes and deployment times. Some key best practices to keep in mind include:

  • Separating the build process from the runtime environment
  • Avoiding unnecessary dependencies in production images
  • Using separate stages for compiling code and copying artifacts
  • Keeping the runtime stage as lean as possible

By following these best practices and leveraging Docker multi-stage builds, you can improve the efficiency and scalability of your production environments.

Top comments (0)