Containerizing a Django application using Docker makes it easier to manage dependencies, deploy consistently, and create a development environment that closely mirrors production. With Docker, you can encapsulate your Django app and its dependencies into a container, which can be run anywhere—whether it’s on your local machine, a cloud server, or during testing.
Why Containerize a Django App?
Consistency: Docker ensures that your app runs the same way across all environments.
Ease of Deployment: With Docker, you can quickly deploy your app to production or any cloud platform.
Simplified Dependency Management: Docker packages all dependencies together, eliminating versioning issues.
Prerequisites
Before we begin, I’ll assume you already know how to set up and run an EC2 instance. You should have:
An EC2 instance running with SSH access.
Docker installed on the EC2 instance
Edit the inbound rules of the security group:
Add a new rule with:
Type: Custom TCP Rule
Port Range: 8000
Source: Anywhere (or a specific IP if you want to limit access).
Steps to Containerize Your Django Application
Clone the Git Repo and navigate to examples → python-web-app
File Structure
python-web-app/ ├── Dockerfile ├── devops/ │ ├── <Your Django app files, like manage.py, settings.py, etc.> ├── requirements.txt
Let’s Write the Docker file
# Use Ubuntu as the base image
FROM ubuntu
# Set environment variable to avoid interactive prompts during package installation
ENV DEBIAN_FRONTEND=noninteractive
# Install system dependencies, including Python 3 and pip
RUN apt-get update && \\
apt-get install -y python3 python3-pip python3-venv && \\
apt-get clean
# Set the working directory inside the container
WORKDIR /app
# Copy the current directory contents (including requirements.txt) into the container at /app
COPY requirements.txt /app
COPY devops /app
# Create a virtual environment and activate it
RUN python3 -m venv /env
# Install Python dependencies inside the virtual environment from requirements.txt
RUN /env/bin/pip install --no-cache-dir -r requirements.txt
#Run the Django development server when the container starts
CMD ["/env/bin/python", "manage.py", "runserver", "0.0.0.0:8000"]
This line sets the base image for the Docker container. Here, you are using
ubuntu
, which means the container will be built from an Ubuntu 20.04 image.This environment variable ensures that when installing packages with
apt-get
, there are no interactive promptsThe Python packages are necessary to run Python applications, particularly Django. Using
y
ensures that the installation happens automatically without waiting for user confirmation.This command sets the working directory for any subsequent
COPY
,RUN
, andCMD
instructions. The working directory will be/app
inside the container.COPY requirements.txt /app
etc, These steps bring the necessary application files (dependencies and project code) into the container so that it can be set up and run.RUN python3 -m venv /env creates a virtual environment for Python within the container, stored in
/env
. A virtual environment helps isolate dependencies for this project, ensuring that packages installed within it don’t interfere with system-wide Python packages.RUN /env/bin/pip install --no-cache-dir -r requirements.txt This installs the Python dependencies (e.g., Django, tzdata, etc.) into the virtual environment
/env
usingpip
. The--no-cache-dir
option preventspip
from saving the downloaded packages, thus reducing the image size.CMD ["/env/bin/python", "manage.py", "runserver", "0.0.0.0:8000"] ,this specifies the default command to run when the container starts. It tells Docker to run the Django development server, which listens on
0.0.0.0:8000
, making the app accessible from outside the container.In this case,
CMD
is preferred because you might want to override the default behavior with another command during development or troubleshooting.
- Now build the docker image
docker build .
Run the Django Application on the Docker container
If you're running your Django application in a Docker container on an EC2 instance, the process to access the Django application from your browser is slightly different because you need to ensure that the EC2 security group allows traffic on the port and that the EC2 instance's IP is used to access the app.
When running the Docker container on your EC2 instance, ensure that you're mapping the container's port
8000
to your EC2 instance's port8000
. This is called Port Mapping.Port mapping is the process of mapping a port on your local machine (host) to a port inside a Docker container. This allows you to access services running inside the container, such as a Django application, from your local machine or any device on the same network.
In this case, the Django development server is running inside the container on port
8000
. You need to map the container's port8000
to a port on your local machine so that you can access the Django application in a web browser.Run the following command on your EC2 instance
docker images #see the docker id
docker run -it -p 8000:8000 <docker-image/id>
#it indicated interactive mode
- Access the Webpage using
http://<your-ec2-public-ip>:8000
Now Navigate to http://<your-ec2-public-ip>:8000 /demo and access the Webpage
Voila you successfully containerized your Django Application 🎉🎊
~ Thank you