From f83f73d6cf0fa411833e48d5b8fccdc36ae81abf Mon Sep 17 00:00:00 2001 From: MonkeyStrongTogether Date: Fri, 22 May 2026 17:57:23 +0200 Subject: [PATCH] Docker --- .dockerignore | 21 +++++++++++ Dockerfile | 42 ++++++++++++++++++++++ Dockerfile.dev | 31 ++++++++++++++++ docker-compose.dev.yml | 21 +++++++++++ docker-compose.yml | 80 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 195 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 Dockerfile.dev create mode 100644 docker-compose.dev.yml create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..497d1e3 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,21 @@ +venv/ +__pycache__/ +*.pyc +*.pyo +*.pyd +.Python +*.so +*.egg +*.egg-info/ +dist/ +build/ +.env +.env.local +.git/ +.gitignore +README.md +tree +db.sqlite3 +*.sqlite3 +.DS_Store +*.log \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3b791b6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,42 @@ +FROM python:3.14-slim-bookworm + +# Set environment variables +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + DJANGO_SETTINGS_MODULE=config.settings + +# Set work directory +WORKDIR /app + +# Install system dependencies +RUN apt-get update && apt-get install -y --no-install-recommends \ + libpq-dev \ + gcc \ + curl \ + && rm -rf /var/lib/apt/lists/* + +# Copy requirements first for better caching +COPY requirements.txt . + +# Install Python dependencies +RUN pip install --no-cache-dir -r requirements.txt + +# Copy project files +COPY . . + +# Create necessary directories +RUN mkdir -p /app/staticfiles /app/static /app/media + +# Collect static files +RUN python manage.py collectstatic --noinput + +# Create a non-root user +RUN addgroup --system app && adduser --system --group app +RUN chown -R app:app /app + +USER app + +EXPOSE 8000 + +# Use Daphne for WebSocket support +CMD ["daphne", "-b", "0.0.0.0", "-p", "8000", "config.asgi:application"] \ No newline at end of file diff --git a/Dockerfile.dev b/Dockerfile.dev new file mode 100644 index 0000000..a51a9ae --- /dev/null +++ b/Dockerfile.dev @@ -0,0 +1,31 @@ + +FROM python:3.14-slim-bookworm + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + DJANGO_SETTINGS_MODULE=config.settings + +WORKDIR /app + +RUN apt-get update && apt-get install -y --no-install-recommends \ + libpq-dev \ + gcc \ + && rm -rf /var/lib/apt/lists/* + +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +# Install development tools +RUN pip install watchdog + +COPY . . + +# Create directory for static files +RUN mkdir -p /app/staticfiles + +# Don't collect static in dev mode (runserver handles it) + +EXPOSE 8000 + +# Run with hot reload using watchdog +CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"] \ No newline at end of file diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml new file mode 100644 index 0000000..a9cb428 --- /dev/null +++ b/docker-compose.dev.yml @@ -0,0 +1,21 @@ +version: '3.8' + +services: + web: + build: + context: . + dockerfile: Dockerfile.dev + ports: + - "8001:8000" + volumes: + - .:/app + - /app/venv # Exclude venv + environment: + - SECRET_KEY=dev-secret-key-change-in-production + - DEBUG=True + - ALLOWED_HOSTS=localhost,127.0.0.1,web + - DATABASE_URL=sqlite:///db.sqlite3 + # No REDIS_URL - will use in-memory channel layer + command: python manage.py runserver 0.0.0.0:8000 + # Or use daphne for WebSocket testing: + # command: daphne -b 0.0.0.0 -p 8000 config.asgi:application \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..dca5902 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,80 @@ +version: '3.8' + +services: + redis: + image: redis:7-alpine + command: redis-server --appendonly yes + volumes: + - redis_data:/data + restart: unless-stopped + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 3s + retries: 5 + networks: + - splitchat_network + + db: + image: postgres:16-alpine + environment: + POSTGRES_DB: splitchat + POSTGRES_USER: splitchat + POSTGRES_PASSWORD: ${DB_PASSWORD:-splitchat_password_123} + volumes: + - postgres_data:/var/lib/postgresql/data + restart: unless-stopped + healthcheck: + test: ["CMD-SHELL", "pg_isready -U splitchat"] + interval: 10s + timeout: 5s + retries: 5 + networks: + - splitchat_network + + web: + build: . + expose: + - "8000" + volumes: + - static_volume:/app/staticfiles + - media_volume:/app/media + environment: + - SECRET_KEY=${SECRET_KEY} + - DEBUG=${DEBUG:-False} + - ALLOWED_HOSTS=${ALLOWED_HOSTS:-localhost,127.0.0.1} + - DATABASE_URL=postgres://splitchat:${DB_PASSWORD:-splitchat_password_123}@db:5432/splitchat + - REDIS_URL=redis://redis:6379/0 + depends_on: + db: + condition: service_healthy + redis: + condition: service_healthy + restart: unless-stopped + networks: + - splitchat_network + + # Optional: Nginx for serving static files (if you want to offload from Django) + nginx: + image: nginx:alpine + ports: + - "8002:80" + volumes: + - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro + - static_volume:/static:ro + - media_volume:/media:ro + depends_on: + - web + restart: unless-stopped + networks: + - splitchat_network + +volumes: + postgres_data: + redis_data: + static_volume: + media_volume: + +networks: + splitchat_network: + driver: bridge \ No newline at end of file