How to Dockerize Your Django Application
ARYAN KYATHAM
Impact Kid | 8x Hackathon Winner | Software Engineer | Startups & AI | Student @FRCRCE |
1. Install Docker Desktop:
2. Understand Containers & Images:
3. Prepare Your Django App for Docker:
pip install gunicorn
pip freeze > requirements.txt
4. Create a Dockerfile:
The Dockerfile contains instructions to build your application image.
FROM python:3.8.3-slim
ENV PYTHONBUFFERED=1 # Useful for debugging
WORKDIR /dock_django
COPY requirements.txt .
RUN pip3 install -r requirements.txt
COPY . .
CMD python manage.py runserver 0.0.0.0:8000 # Make the app accessible outside the container
Base Image: Choose a Python version from Docker Hub. Use slim variants (e.g., 3.8.3-slim) for smaller images.
5. Create a docker-compose.yaml File:
This file orchestrates multiple containers (if needed) and simplifies the build and run process.
version: "3.8"
services:
app:
build: .
volumes:
- .:/dock_django
ports:
- 8000:8000
image: app:dock_django
container_name: tanjiro_container
command: python manage.py runserver 0.0.0.0:8000
6. Build and Run the Container
Run the following commands:
docker-compose build
docker-compose up
7. Environment Management
SECRET_KEY=your_secret_key
POSTGRES_USER=your_db_username
POSTGRES_PASSWORD=your_db_password
POSTGRES_DB=your_db_name
POSTGRES_HOST=your_db_host
#settings.py
import os
SECRET_KEY = os.environ.get('SECRET_KEY')
DB_USERNAME = os.environ.get('POSTGRES_USER')
DB_PASSWORD = os.environ.get('POSTGRES_PASSWORD')
DB_DATABASE = os.environ.get('POSTGRES_DB')
DB_HOST = os.environ.get('POSTGRES_HOST')
8. Clean Up Cache
Free up space by pruning unused Docker resources:
docker system prune
Deploy Dockerized Django App on AWS with ECR and ECS
Step 1: Create and Push Docker Image to AWS ECR
1.Create an ECR Repository:
aws ecr create-repository --repository-name my-django-app
2. Authenticate Docker with ECR:
aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <account_id>.dkr.ecr.<region>.amazonaws.com
3. Build Docker Image:
docker build -t my-django-app .
4. Tag the Docker Image:
docker tag my-django-app:latest <account_id>.dkr.ecr.<region>.amazonaws.com/my-django-app:latest
5. Push Image to ECR:
docker push <account_id>.dkr.ecr.<region>.amazonaws.com/my-django-app:latest
Step 2: Create a Task Definition for ECS
Create a JSON file (e.g., task-definition.json) with the following content:
{
"containerDefinitions": [
{
"name": "my-django-application",
"image": "<account_id>.dkr.ecr.<region>.amazonaws.com/my-django-app:latest",
"memory": "256",
"cpu": "256",
"essential": true
}
],
"networkMode": "bridge",
"family": "django-app-task-definition"
}
Step 3: Deploy to ECS
aws ecs register-task-definition --cli-input-json file://task-definition.json
aws ecs create-service \
--cluster <cluster-name> \
--service-name my-django-app-service \
--task-definition django-app-task-definition \
--desired-count 1
Step 4: Configure ECS Cluster (EC2 Instance)
Your Django app should now be running on AWS ECS, powered by your Dockerized image from ECR!
10. Cluster Setup and Communication
11. Application Deployment
kubectl apply -f deployment.yaml # Deploys the app
kubectl get deployments # Verifies the deployment
kubectl get pods # Checks running pods
kubectl exec -t <podname> -- /bin/bash # Access pod shell
12. Networking and Exposing Services
kubectl get services
# OUTPUT:
# NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S)
# nginx-service LoadBalancer 10.245.166.184 161.35.266.32 80:31988/TCP
13. Scaling and Resource Management
kubectl scale deployment nginx-deployment --replicas=5
kubectl get pods # Verify new pods
kubectl set image deployment/nginx-deployment nginx=nginx:latest
Junior Developer | Building Skills in Full-Stack Development & Problem-Solving | Young enthusiast | Lend A Hand India-Non-Profit Org
3 个月This seems pretty interesting! I have some questions tho, can you please accept my request??
Frontend Developer | IT Student | DSA | Leetcode | Writer | @icodeguru | Shaper at Global Shapers Community | Member at World Economic Forum
3 个月You’re doing an amazing job! I’m truly inspired by how you excel and win every hackathon. I have a question—did you learn all your skills on campus, or did you develop them on your own?
B.Tech CSE Student | Tech Enthusiast | Budding Software Developer | Passionate About AI, Machine Learning, and Innovative Problem-Solving
3 个月This feels new and motivating... Dockerizing and deployment, will see more into it..