Multi Stage Docker Builds - Reduce Image Size by 89 %
in above image - green tick marked image is created with multi stage- with mimimal size and red check mark image is build without distroless image/ multi stage docker which is bigger in size.
Understanding Multi-Stage Docker Builds
Multi-stage Docker builds are a powerful approach to create lean and efficient container images. They involve defining multiple stages or phases in a Dockerfile, each with a specific purpose. Each stage can be seen as a temporary container that serves a particular function, such as compiling code, running tests, or packaging the final application.
The key advantage of multi-stage builds is that you can include all the necessary build tools and dependencies in one stage while copying only the essential artifacts into the final image. This results in smaller and more s
Stage 1: Build Stage
FROM ubuntu AS Build
WORKDIR /app
COPY requirements.txt /app
COPY devops /app
RUN apt-get update && \
apt-get install -y python3 python3-pip && \
pip install -r requirements.txt && \
cd devops
# Stage 2: Final Stage
# Using a Distroless image for Python
FROM gcr.io/distroless/python3
# Copy the contents of the "Build" stage into the "Final" stage
COPY --from=Build /app /app
# Set the entry point and command to run the application
ENTRYPOINT ["python3"]
CMD ["manage.py", "runserver", "0.0.0.0:8000"]
Here's how this Dockerfile works:
Stage 1 (Build Stage):
It starts with an Ubuntu base image and is named "Build."
Sets the working directory to
/app
.Copies the
requirements.txt
file and thedevops
directory into the/app
directory.Updates the package index and installs Python 3 and pip.
Installs the Python dependencies listed in
requirements.txt
.Changes the working directory to the
devops
directory (although this step might not be necessary since the subsequent stages use a different image).
Stage 2 (Final Stage):
Uses a Distroless image for Python (
gcr.io/distroless/python3
) as the final base image. Distroless images are minimalistic and contain only essential components for running applications.Copies the contents of the "Build" stage, including the installed Python dependencies and application files, from
/app
in the "Build" stage to/app
in the "Final" stage.Sets the entry point for the container to
python3
.Specifies the default command to run the application using
manage.py
to start a development server listening on0.0.0.0:8000
.check my full repo -https://github.com/krishkprawat/Multi-stage-build-docker/blob/main/README.md
Advantages of Smaller Container Images
Reducing your container image size comes with several key benefits:
Faster Deployments: Smaller images mean faster image pulls and deployments, reducing the time it takes to scale your services.
Less Bandwidth Consumption: Smaller images consume less network bandwidth, making them more suitable for cloud-based deployments, especially when you pay for data transfer.
Improved Security: Smaller images have a reduced attack surface, making it more challenging for attackers to find vulnerabilities to exploit.
Simplified Management: Smaller images are easier to manage, as there are fewer components to update, patch, and maintain.
Thanks for reading....
Subscribe to my newsletter
Read articles from krishnapal rawat directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
krishnapal rawat
krishnapal rawat
Pushing code to its limits, one test at a time - I'm a QA engineer with a passion for coding and testing