Swipe left or right to navigate to next or previous post
Before we dive into the multi-stage build, let's get familiar with some of the terms related to Dockerization.
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:
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 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:
requirements.txt
.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
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:
your_ecr_repository_url/your_image_name:tag
).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.
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
docker build -t your-ecr-repository-uri:your-image-tag .
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
docker push your-aws-account-id.dkr.ecr.your-aws-region.amazonaws.com/your-ecr-repository-name:your-image-tag