Swipe left or right to navigate to next or previous post

Extensive guide on multi-stage build for django app

15 Apr 2024 . category: Programming . Comments
#Architecture #Programming #Software

Extensive guide on multi-stage build for django app - Tapan BK

Before we dive into the multi-stage build, let's get familiar with some of the terms related to Dockerization.

About Docker

Docker is a platform that enables developers to develop, ship, and run applications using containerization. Containers are lightweight, standalone, and executable packages that contain everything needed to run an application, including code, runtime, system tools, libraries, and settings. Docker provides tools and APIs for building, managing, and deploying containers.

Here's a brief overview of key Docker concepts:

  1. Image: An image is a read-only template that contains a set of instructions for creating a container. It includes the application code, runtime, libraries, dependencies, and other necessary components. Images are built using a Dockerfile.
  2. Container: A container is a runtime instance of an image. It encapsulates the application and its dependencies, ensuring consistency and portability across different environments. Containers are isolated from each other and from the underlying host system, providing security and resource efficiency.
  3. Dockerfile: A Dockerfile is a text file that contains instructions for building a Docker image. It specifies the base image, environment variables, commands to run during the build process, and other configuration settings.
  4. docker build: docker build is a command-line tool used to build Docker images from a Dockerfile. It reads the instructions in the Dockerfile, executes them step by step, and generates an image. The resulting image can then be used to create containers.

What is multi-stage build?

Multi-stage builds in Docker allow you to create more efficient Dockerfiles by reducing the size of your final image. This is achieved by splitting the build process into multiple stages, where each stage produces an intermediate image that is used as the base for the next stage. This way, you can include only the necessary dependencies and files in each stage, resulting in a smaller final image.

Multi-stage build for Django

Multi-stage builds can also be utilized for Django applications to optimize Docker images.


# Stage 1: Install dependencies
FROM python:3.9 as builder

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set working directory
WORKDIR /app

# Copy requirements file
COPY requirements.txt .

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

# Stage 2: Copy application code and create final image
FROM python:3.9-slim

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set working directory
WORKDIR /app

# Copy installed dependencies from the builder stage
COPY --from=builder /usr/local/lib/python3.9/site-packages/ /usr/local/lib/python3.9/site-packages/

# Copy the rest of the application code
COPY . .

# Clean up temporary files
RUN apt-get update \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* \
    && rm -rf /tmp/*

# Expose port
EXPOSE 8000

# Specify the command to run the application
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "myproject.wsgi:application"]

In this Dockerfile:

  • Stage 1 (builder): Installs dependencies specified in requirements.txt.
  • Stage 2: Copies only the application code and installed dependencies from the builder stage. This includes the installed Python packages and the Django application code.

Ensure to replace "myproject.wsgi:application" with your actual project's WSGI application.

To build and run your Django Docker image:


docker build -t my-django-app .
docker run --rm -p 8000:8000 my-django-app

This Dockerfile optimizes the image size by using a slim Python image for the final stage while still providing all necessary dependencies to run a Django application. This is one of the way for multi-stage build

Get pre-built image from the ecr or docker hub

To use a pre-built image from Amazon ECR as the base image for a multi-stage build, you'll need to reference the image's URI in your Dockerfile. Here's how you can achieve this:


# Stage 1: Use the pre-built image from ECR as base image
FROM your_ecr_repository_url/your_image_name:tag as base

# Stage 2: Copy application code and create final image
FROM base as final

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set working directory
WORKDIR /app

# Copy the rest of the application code
COPY . .

# Clean up temporary files
RUN apt-get update \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* \
    && rm -rf /tmp/*

# Expose port
EXPOSE 8000

# Specify the command to run the application
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "myproject.wsgi:application"]

In this Dockerfile:

  • Stage 1: Uses the pre-built image from Amazon ECR as the base image, referencing its URI (your_ecr_repository_url/your_image_name:tag).
  • Stage 2: Inherits from the base image and continues the build process by copying the application code and defining the final image configuration.

Make sure to replace your_ecr_repository_url, your_image_name, and tag with the appropriate values matching your ECR repository and Docker image.

Once you've created your Dockerfile, you can build the Docker image using the docker build command. This approach allows you to leverage pre-built images stored in Amazon ECR as the base for your multi-stage builds.

Let's recap about the pushing the docker build to ecr.

Push the image to ecr

  • Authenticate Docker to your Amazon ECR registry.
  • Build your Docker image.
  • Tag your Docker image with the ECR repository URI.
  • Push your Docker image to Amazon ECR.

Authenticate Docker to your Amazon ECR registry:


aws ecr get-login-password --region your-aws-region | docker login --username AWS --password-stdin your-aws-account-id.dkr.ecr.your-aws-region.amazonaws.com/your-ecr-repository-name

Build your Docker image:


docker build -t your-ecr-repository-uri:your-image-tag .

Tag your Docker image:


docker tag your-ecr-repository-uri:your-image-tag your-aws-account-id.dkr.ecr.your-aws-region.amazonaws.com/your-ecr-repository-name:your-image-tag

Push your Docker image to Amazon ECR:


docker push your-aws-account-id.dkr.ecr.your-aws-region.amazonaws.com/your-ecr-repository-name:your-image-tag


Tapan B.K. | Full Stack Software Engineer

Tapan B.K. is Full Stack Software Engineer. In his spare time, Tapan likes to watch movies, visit new places.