feat: prepare production Docker deployment

This commit is contained in:
Денис Братусев committed 2026-10-29 17:06:38 +03:00
1 parent 36b1341a10
commit d4cf42cb5e
5 files changed
+99 -16

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
.venv/
__pycache__/
*.py[cod]
.git/
.env
.env.*
!.env.example
media/
staticfiles/
.pytest_cache/
.coverage
.env.prod
.env.prod.*
+16
View File
@@ -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"]
+27 -16
View File
@@ -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 = "/"
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"
+42
View File
@@ -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:
BIN
View File
Binary file not shown.