Django project with Docker and Kubernetes

Pranav Bakare - Sep 16 - - Dev Community

To set up a simple Django project with Docker and Kubernetes, follow these steps:

  1. Create a Django Project

First, start by setting up your Django project.

  1. Install Django and create a new project:

pip install django
django-admin startproject myproject
cd myproject

  1. Run the development server to confirm the project works:

python manage.py runserver

  1. 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"]

  1. Create requirements.txt

To ensure the correct dependencies, create a requirements.txt file for Django:

requirements.txt

Django>=3.0,<4.0

  1. 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

  1. Build and Run the Docker Container

  2. Build the Docker image:

docker build -t my-django-app .

  1. 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.


  1. Kubernetes Setup

To run this application on Kubernetes, you’ll need to create a Kubernetes deployment and service configuration.

  1. 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

  1. 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
  1. Deploy to Kubernetes

  2. Ensure you have a Kubernetes cluster running (e.g., Minikube or any cloud provider like AWS, GCP).

  3. Apply the deployment and service:

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml

  1. Check if the pods are running:

kubectl get pods

  1. Once the service is up, find the external IP:

kubectl get services

Your Django app should now be running on Kubernetes!


Summary of Steps:

  1. Dockerize the Django app using a Dockerfile.

  2. (Optional) Use Docker Compose for local setup.

  3. Create Kubernetes deployment and service YAML files.

  4. Deploy to Kubernetes using kubectl.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .