添加docker-compose.prod

This commit is contained in:
xie7654
2025-07-04 22:52:22 +08:00
parent 87d8d5ffb4
commit d705a88bd3
19 changed files with 536 additions and 70 deletions

62
backend/.dockerignore Normal file
View File

@@ -0,0 +1,62 @@
**/.idea/
.idea/**
**/*.pyc
__pycache__/
build/
*.egg-info/
.python-version
.pytest_cache/
eggs/
lib/
lib64/
.DS_Store
docs/_build/
*.env
**/local_settings.py
static/
.nitro
.output
*-dist.zip
*-dist.tar
*-dist.war
coverage
*.local
**/.vitepress/cache
.cache
.turbo
.temp
dev-dist
.stylelintcache
yarn.lock
package-lock.json
.VSCodeCounter
**/backend-mock/data
# local env files
.env.local
.env.*.local
.eslintcache
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
vite.config.mts.*
vite.config.mjs.*
vite.config.js.*
vite.config.ts.*
# Editor directories and files
.idea
# .vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
.history
celerybeat-schedule.db

View File

@@ -1,5 +1,5 @@
# syntax=docker/dockerfile:1
FROM python:3.12.2 AS dev
FROM python:3.12.2 AS base
WORKDIR /app
@@ -14,8 +14,20 @@ RUN apt-get update && \
COPY . .
RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple
# 入口命令由 docker-compose 控制
# 收集静态文件
RUN python manage.py collectstatic --noinput
CMD ["sh", "-c", "sleep 10 && python manage.py runserver 0.0.0.0:8000"]
# 默认命令,开发和生产通过 docker-compose 覆盖
#CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
#CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
FROM base AS dev
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
# CMD ["sh", "-c", "sleep 5 && python manage.py runserver 0.0.0.0:8000"]
FROM base AS prod

View File

@@ -23,7 +23,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = 'django-insecure-m4@pv814c_m^pgpyhz^i96a@mcqh_@m9ccu(17*895t!79e!nb'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = os.getenv('DEBUG', 'False') == 'True'
ALLOWED_HOSTS = [
'*',
@@ -92,9 +92,9 @@ DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_vue',
'USER': 'root',
'PASSWORD': 'my-secret-pw',
'HOST': os.getenv('DATABASE_HOST', 'localhost'),
'USER': os.getenv('DB_USER', 'chenze'),
'PASSWORD': os.getenv('DB_PASSWORD', 'my-secret-pw'),
'HOST': os.getenv('DB_HOST', 'localhost'),
# 'HOST': 'localhost',
}
}
@@ -168,9 +168,24 @@ REST_FRAMEWORK = {
]
}
# celery 配置
CELERY_BROKER_URL = 'redis://localhost:6379/6'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/6'
# ================= Redis 缓存配置 =================
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": f"redis://{os.environ.get('REDIS_HOST', 'localhost')}:{os.environ.get('REDIS_PORT', 6379)}/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}
# SESSION 存 Redis
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "default"
# ================= Celery 配置(适配环境变量) =================
CELERY_BROKER_URL = f"redis://{os.environ.get('REDIS_HOST', 'localhost')}:{os.environ.get('REDIS_PORT', 6379)}/0"
CELERY_RESULT_BACKEND = CELERY_BROKER_URL
# 时区设置
CELERY_TIMEZONE = 'Asia/Shanghai'
# 任务序列化方式

View File

@@ -12,3 +12,5 @@ redis==6.2.0
eventlet==0.40.0
goofish_api==0.0.6
flower==2.0.1
gunicorn==23.0.0
django_redis==6.0.0