Skip to content

Docker Setup

# Use official Python runtime as base image
FROM python:3.11-slim
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV DEBUG=False
# Set work directory
WORKDIR /app
# Install system dependencies
RUN 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 dependencies
COPY requirements.txt .
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
# Copy project
COPY . .
# Create and switch to non-root user
RUN useradd --create-home --shell /bin/bash appuser
RUN chown -R appuser:appuser /app
USER appuser
# Expose port
EXPOSE 8000
# Run application
CMD ["gunicorn", "config.wsgi:application", "--bind", "0.0.0.0:8000"]
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