Docker Setup
5. Docker Containerization
Section titled “5. Docker Containerization”Step 1: Create Dockerfile
Section titled “Step 1: Create Dockerfile”Create Dockerfile in project root
Section titled “Create Dockerfile in project root”# Use official Python runtime as base imageFROM python:3.11-slim
# Set environment variablesENV PYTHONDONTWRITEBYTECODE 1ENV PYTHONUNBUFFERED 1ENV DEBUG=False
# Set work directoryWORKDIR /app
# Install system dependenciesRUN apt-get update \ && apt-get install -y --no-install-recommends \ postgresql-client \ gcc \ python3-dev \ musl-dev \ && rm -rf /var/lib/apt/lists/*
# Install Python dependenciesCOPY requirements.txt .RUN pip install --upgrade pipRUN pip install -r requirements.txt
# Copy projectCOPY . .
# Create and switch to non-root userRUN useradd --create-home --shell /bin/bash appuserRUN chown -R appuser:appuser /appUSER appuser
# Expose portEXPOSE 8000
# Run applicationCMD ["gunicorn", "config.wsgi:application", "--bind", "0.0.0.0:8000"]Step 2: Create Docker Compose File
Section titled “Step 2: Create Docker Compose File”Create docker-compose.yml
Section titled “Create docker-compose.yml”version: "3.8"
services: web: build: . command: > sh -c "python manage.py migrate && python manage.py collectstatic --noinput && gunicorn config.wsgi:application --bind 0.0.0.0:8000" volumes: - static_volume:/app/staticfiles - media_volume:/app/media expose: - "8000" environment: - DEBUG=False - DB_NAME=postgres - DB_USER=postgres - DB_PASSWORD=postgres - DB_HOST=db - DB_PORT=5432 - REDIS_URL=redis://redis:6379/0 depends_on: - db - redis networks: - django-network
db: image: postgres:15 volumes: - postgres_data:/var/lib/postgresql/data/ environment: - POSTGRES_DB=postgres - POSTGRES_USER=postgres - POSTGRES_PASSWORD=postgres networks: - django-network
redis: image: redis:7-alpine volumes: - redis_data:/data networks: - django-network
celery: build: . command: celery -A config worker --loglevel=info volumes: - .:/app environment: - DEBUG=False - DB_NAME=postgres - DB_USER=postgres - DB_PASSWORD=postgres - DB_HOST=db - DB_PORT=5432 - REDIS_URL=redis://redis:6379/0 depends_on: - db - redis networks: - django-network
celery-beat: build: . command: celery -A config beat --loglevel=info volumes: - .:/app environment: - DEBUG=False - DB_NAME=postgres - DB_USER=postgres - DB_PASSWORD=postgres - DB_HOST=db - DB_PORT=5432 - REDIS_URL=redis://redis:6379/0 depends_on: - db - redis networks: - django