This project demonstrates how to containerize a Django app using Docker and run it on an AWS EC2 instance.
This project outlines the steps to containerize a Django app using Docker and deploy it on an AWS EC2 instance. We will create a Dockerfile, build a Docker image, and run the container on an EC2 instance.
- Basic knowledge of Django, Docker, and AWS EC2
- AWS account with permissions to create EC2 instances
- A GitHub account (optional) for storing project files
The Dockerfile defines the environment and dependencies required to run your Django app. Here’s a basic Dockerfile:
# Use the official Python image with Python and pip pre-installed
FROM python:3.9-slim
# Set the working directory inside the container
WORKDIR /app
# Copy the requirements file and the devops directory into the container
COPY requirements.txt /app
COPY devops /app
# Update package lists and install system dependencies (optional)
RUN apt-get update -y && apt-get install -y build-essential
# Install Python dependencies from requirements.txt
RUN pip install -r requirements.txt
# Set Python3 as the entry point
ENTRYPOINT ["python3"]
# Run the Django development server by default
CMD ["manage.py", "runserver", "0.0.0.0:8000"]
Once the Dockerfile and related files are ready, push them to your GitHub repository.
- Create an EC2 instance in the AWS region of your choice.
- Generate a key-pair to enable SSH access.
- Configure the security group:
- Allow inbound SSH on port 22 (restrict to your IP or "Anywhere" for learning purposes).
- Open custom TCP port 8000 to allow access to the Django app.
Use the following commands to connect to your EC2 instance. Make sure to set correct permissions on your .pem
file:
chmod 400 ~/path-to-pem-file.pem
ssh -i ~/path-to-pem-file.pem ec2-user@your-ec2-ip-address
Update the package manager:
sudo yum update -y # For Amazon Linux
sudo apt-get update -y # For Ubuntu
Install Docker with the following commands:
sudo yum install docker -y # For Amazon Linux
sudo apt-get install docker.io -y # For Ubuntu
Add your user to the docker
group:
sudo usermod -aG docker ec2-user
Log out and log back in for the permissions to take effect.
Clone the project repository from GitHub:
git clone <your-github-repository-url>
Start the Docker service:
sudo systemctl start docker
Check if Docker is running by using:
docker ps
To build and run your Docker image, navigate to the project directory and run:
docker build -t <your-dockerhub-username>/django-app:latest .
This will build the Docker image based on your Dockerfile. After the image is built, you can run it using the appropriate port settings.
You can verify Docker installation by running:
docker ps
docker images
If the image is built successfully, run the following command to get it running and confirm it's running in the browser:
docker run -t -p 8000:8000 <image-name-under-docker-images-commands>
Head to your browser and check if the app is running at:
http://<your-ip-address>:8000/demo/
If it’s running, congratulations! You have successfully containerized your first Django app with Docker.
If it’s not running or you encounter any issues, please review the steps and feel free to reach out for help.
This project is licensed under the MIT License.