Dockerize Django Project
Start a Django Project
django-admin startproject myproject
cd myproject
myproject/
├── manage.py
└── myproject/
├── __init__.py
├── asgi.py
├── settings.py
├── urls.py
└── wsgi.py
Add a requirements.txt
create a file called requirements.txt
Django>=4.0,<5.0
generate a full list of dependencies from your local virtual environment
pip freeze > requirements.txt
Create a Dockerfile
create a file called Dockerfile
# Use the official Python image
FROM python:3.10-slim
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# Set the working directory in the container
WORKDIR /app
# Install dependencies
COPY requirements.txt /app/
RUN pip install --upgrade pip && pip install -r requirements.txt
# Copy the rest of the code
COPY . /app/
Test & run locally with docker-compose
Create docker-compose.yml
version: '3.9'
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/app
ports:
- "8000:8000"
docker-compose up --build
Reference
https://www.freecodecamp.org/news/how-to-dockerize-your-django-project/