To set up a simple Django project with Docker and Kubernetes, follow these steps:
- Create a Django Project
First, start by setting up your Django project.
- Install Django and create a new project:
pip install django
django-admin startproject myproject
cd myproject
- Run the development server to confirm the project works:
python manage.py runserver
- Create a Dockerfile
Create a Dockerfile in the root of your Django project. This file defines the image for your Django app.
Dockerfile
Use official Python image from DockerHub
FROM python:3.9-slim
Set the working directory in the container
WORKDIR /app
Copy requirements.txt to the working directory
COPY requirements.txt .
Install any needed packages
RUN pip install -r requirements.txt
Copy the current directory contents into the container at /app
COPY . .
Make port 8000 available to the world outside this container
EXPOSE 8000
Define environment variable
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
Run the command to start the Django app
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
- Create requirements.txt
To ensure the correct dependencies, create a requirements.txt file for Django:
requirements.txt
Django>=3.0,<4.0
- Create a docker-compose.yml (Optional)
If you want to use Docker Compose for easier setup, create the following docker-compose.yml file:
version: '3'
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/app
ports:
- "8000:8000"
depends_on:
- db
db:
image: postgres:13
environment:
POSTGRES_DB: myproject
POSTGRES_USER: user
POSTGRES_PASSWORD: password
Build and Run the Docker Container
Build the Docker image:
docker build -t my-django-app .
- Run the Docker container:
docker run -p 8000:8000 my-django-app
If you use Docker Compose:
docker-compose up
Your Django app should now be running at http://localhost:8000.
- Kubernetes Setup
To run this application on Kubernetes, you’ll need to create a Kubernetes deployment and service configuration.
- Create Kubernetes Deployment YAML (deployment.yaml):
apiVersion: apps/v1
kind: Deployment
metadata:
name: django-app
spec:
replicas: 2
selector:
matchLabels:
app: django-app
template:
metadata:
labels:
app: django-app
spec:
containers:
- name: django-app
image: my-django-app:latest
ports:
- containerPort: 8000
- Create Kubernetes Service YAML (service.yaml):
apiVersion: v1
kind: Service
metadata:
name: django-service
spec:
selector:
app: django-app
ports:
- protocol: TCP port: 8000 targetPort: 8000 type: LoadBalancer
Deploy to Kubernetes
Ensure you have a Kubernetes cluster running (e.g., Minikube or any cloud provider like AWS, GCP).
Apply the deployment and service:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
- Check if the pods are running:
kubectl get pods
- Once the service is up, find the external IP:
kubectl get services
Your Django app should now be running on Kubernetes!
Summary of Steps:
Dockerize the Django app using a Dockerfile.
(Optional) Use Docker Compose for local setup.
Create Kubernetes deployment and service YAML files.
Deploy to Kubernetes using kubectl.