Docker: Multi-Stage Builds

Docker: Multi-Stage Builds

Multi-stage builds in Docker allow you to create more efficient Docker images by using multiple build stages within a single Dockerfile. Each stage can execute a specific set of instructions, and you can copy artifacts or files from one stage to another. This helps reduce the final size of the Docker image by excluding unnecessary build dependencies and intermediate files.

Let’s understand it through an example

Here I have an image already called flask-app the size of it is 1.01 GB. It uses pyhton3.9 as it’s base image which itself is of 997 MB.

# Base Image
FROM python:3.9 AS backend-builder
# Set working directory
WORKDIR /app
# Copy the source files to the container's working directory
COPY . .
# Install dependencies
RUN pip install -r requirements.txt
# Run the Python application
CMD ["python", "run.py"]

Build docker image using this Dockerfile: docker build -t flask-app .

Now do, docker images. Check the size of the image it would be 1.01 GB

When building Docker images, it’s important to keep the size of the image as small as possible to improve deployment speed, reduce storage costs, and enhance security. One way to achieve this is by using multi-stage builds in Dockerfiles.

Multi-stage builds allow you to use multiple FROM statements in a single Dockerfile, enabling you to separate the build environment from the runtime environment. This allows you to install build dependencies, compile code, and perform other tasks in one stage, and then copy only the necessary artifacts into the final stage, resulting in a smaller and more efficient image.

Here’s how you can use multi-stage builds to reduce the size of your Flask application Docker image:

· Stage 1 (backend-builder): This stage is responsible for building the application. It uses a full Python image to install dependencies and perform any other build tasks specified in the Dockerfile. Once the build is complete, it creates a snapshot of the dependencies and application code.

· Stage 2: This stage is used for the runtime environment. It starts with a smaller Python alpine image, which only includes essential components needed to run Python applications. It copies the dependencies installed in the backend-builder stage (/usr/local/lib/python3.9/site-packages) and the application code into the final image. This results in a much smaller image size compared to using a full Python image for runtime.

You can build the docker image: docker build -t flask-app:alpine .

Run docker container: docker run flask-app:alpine

By using multi-stage builds, you can significantly reduce the size of your Docker image while still ensuring that all necessary dependencies are included for running the Flask application. This approach helps optimize the deployment process and reduces the overall resource usage.

GitHub Link for the project “Flask App”:

GitHub - RuchikaPatel/flask-app at master
Contanerizing Flask Application. Contribute to RuchikaPatel/flask-app development by creating an account on GitHub.
github.com