From d4cf42cb5ee32260ac41faef90a1fefc9984c3ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B5=D0=BD=D0=B8=D1=81=20=D0=91=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D1=83=D1=81=D0=B5=D0=B2?= <55459712+bratusev07@users.noreply.github.com> Date: Thu, 29 Oct 2026 17:06:38 +0300 Subject: [PATCH] feat: prepare production Docker deployment --- .dockerignore | 14 +++++++++++++ Dockerfile | 16 +++++++++++++++ config/settings.py | 43 +++++++++++++++++++++++++--------------- docker-compose.prod.yml | 42 +++++++++++++++++++++++++++++++++++++++ requirements.txt | Bin 298 -> 380 bytes 5 files changed, 99 insertions(+), 16 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.prod.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..8d60b44 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,14 @@ + +.venv/ +__pycache__/ +*.py[cod] +.git/ +.env +.env.* +!.env.example +media/ +staticfiles/ +.pytest_cache/ +.coverage +.env.prod +.env.prod.* \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ed68353 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ + +FROM python:3.12-slim + +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PYTHONUNBUFFERED=1 + +WORKDIR /app + +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +COPY . . + +EXPOSE 8000 + +CMD ["gunicorn", "config.wsgi:application", "--bind", "0.0.0.0:8000", "--workers", "2"] diff --git a/config/settings.py b/config/settings.py index 9e1bec2..957fce2 100644 --- a/config/settings.py +++ b/config/settings.py @@ -12,20 +12,24 @@ https://docs.djangoproject.com/en/5.2/ref/settings/ from pathlib import Path -# Build paths inside the project like this: BASE_DIR / 'subdir'. +import os +from dotenv import load_dotenv + BASE_DIR = Path(__file__).resolve().parent.parent +load_dotenv(BASE_DIR / ".env") + # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/ -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-k9wiu2ueyzfd_ae%*(q4i)(%40+rk4t4w(s2-#6y@t&dzb4dcc' - -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True - -ALLOWED_HOSTS = [] +SECRET_KEY = os.getenv("DJANGO_SECRET_KEY", "") +DEBUG = os.getenv("DJANGO_DEBUG", "False").lower() == "true" +ALLOWED_HOSTS = [ + host.strip() + for host in os.getenv("DJANGO_ALLOWED_HOSTS", "").split(",") + if host.strip() +] # Application definition @@ -77,11 +81,11 @@ WSGI_APPLICATION = 'config.wsgi.application' DATABASES = { "default": { "ENGINE": "django.db.backends.postgresql", - "NAME": "skb-market", - "USER": "skb-market", - "PASSWORD": "skb-market-password", - "HOST": "127.0.0.1", - "PORT": "5432", + "NAME": os.getenv("DB_NAME", "skb-market"), + "USER": os.getenv("DB_USER", "skb-market"), + "PASSWORD": os.getenv("DB_PASSWORD", ""), + "HOST": os.getenv("DB_HOST", "127.0.0.1"), + "PORT": os.getenv("DB_PORT", "5432"), } } @@ -120,18 +124,25 @@ USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/5.2/howto/static-files/ -STATIC_URL = 'static/' +STATIC_URL = "/static/" +STATIC_ROOT = BASE_DIR / "staticfiles" # Default primary key field type # https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' -import os MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/' LOGIN_URL = "login" LOGIN_REDIRECT_URL = "/" -LOGOUT_REDIRECT_URL = "/" \ No newline at end of file +LOGOUT_REDIRECT_URL = "/" + +# Production security +if not DEBUG: + SECURE_CONTENT_TYPE_NOSNIFF = True + SESSION_COOKIE_SECURE = True + CSRF_COOKIE_SECURE = True + X_FRAME_OPTIONS = "DENY" \ No newline at end of file diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml new file mode 100644 index 0000000..8d6b67c --- /dev/null +++ b/docker-compose.prod.yml @@ -0,0 +1,42 @@ + +services: + db: + image: postgres:17 + restart: unless-stopped + environment: + POSTGRES_DB: ${DB_NAME:?Set DB_NAME in .env.prod} + POSTGRES_USER: ${DB_USER:?Set DB_USER in .env.prod} + POSTGRES_PASSWORD: ${DB_PASSWORD:?Set DB_PASSWORD in .env.prod} + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"] + interval: 10s + timeout: 5s + retries: 5 + + web: + build: . + restart: unless-stopped + env_file: + - .env.prod + environment: + DB_HOST: db + DB_PORT: "5432" + command: > + sh -c "python manage.py migrate && + python manage.py collectstatic --noinput && + gunicorn config.wsgi:application --bind 0.0.0.0:8000 --workers 2" + depends_on: + db: + condition: service_healthy + expose: + - "8000" + volumes: + - staticfiles:/app/staticfiles + - mediafiles:/app/media + +volumes: + postgres_data: + staticfiles: + mediafiles: diff --git a/requirements.txt b/requirements.txt index 2b75656707e7595d465afb072f23db344f03d88e..faa65fcea43de3092083888e7f4610fe1cf5b26d 100644 GIT binary patch delta 90 zcmZ3*^oMDK7Gn?>Ljgl2LkU9$Lq0