test
This commit is contained in:
46
Dockerfile
46
Dockerfile
@@ -1,17 +1,41 @@
|
|||||||
FROM python:3.12-alpine
|
FROM python:3.12-slim as builder
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
# 安装构建依赖
|
||||||
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
|
build-essential \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
# 复制依赖文件
|
||||||
|
COPY requirements.txt .
|
||||||
|
|
||||||
|
# 使用国内镜像源安装依赖
|
||||||
|
RUN pip config set global.index-url https://mirrors.ustc.edu.cn/pypi/web/simple \
|
||||||
|
&& pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
# 最终阶段
|
||||||
|
FROM python:3.12-slim
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# 创建非root用户
|
||||||
|
RUN useradd -m -u 1000 appuser
|
||||||
|
|
||||||
|
# 从builder阶段复制Python包
|
||||||
|
COPY --from=builder /usr/local/lib/python3.12/site-packages/ /usr/local/lib/python3.12/site-packages/
|
||||||
|
COPY --from=builder /usr/local/bin/ /usr/local/bin/
|
||||||
|
|
||||||
|
# 复制应用代码
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
# 设置权限
|
||||||
|
RUN chown -R appuser:appuser /app \
|
||||||
|
&& chmod +x /app/entrypoint.sh
|
||||||
|
|
||||||
|
# 切换到非root用户
|
||||||
|
USER appuser
|
||||||
|
|
||||||
EXPOSE 8000
|
EXPOSE 8000
|
||||||
|
|
||||||
COPY requirements.txt /app
|
|
||||||
|
|
||||||
RUN pip config set global.index-url https://mirrors.ustc.edu.cn/pypi/web/simple
|
|
||||||
|
|
||||||
RUN pip install --no-cache-dir -r requirements.txt
|
|
||||||
|
|
||||||
COPY . /app
|
|
||||||
|
|
||||||
RUN chmod +x /app/entrypoint.sh
|
|
||||||
|
|
||||||
ENTRYPOINT ["/app/entrypoint.sh"]
|
ENTRYPOINT ["/app/entrypoint.sh"]
|
||||||
@@ -1,13 +1,31 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
|
# 等待数据库就绪
|
||||||
|
echo "Waiting for database..."
|
||||||
sleep 5
|
sleep 5
|
||||||
|
|
||||||
# 执行数据库迁移
|
# 执行数据库迁移
|
||||||
|
echo "Running database migrations..."
|
||||||
python manage.py migrate --noinput
|
python manage.py migrate --noinput
|
||||||
|
|
||||||
|
# 收集静态文件
|
||||||
|
echo "Collecting static files..."
|
||||||
|
python manage.py collectstatic --noinput
|
||||||
|
|
||||||
|
# 计算worker数量 (CPU核心数 * 2 + 1)
|
||||||
|
WORKERS=$(python -c 'import multiprocessing; print(multiprocessing.cpu_count() * 2 + 1)')
|
||||||
|
|
||||||
# 启动 Gunicorn
|
# 启动 Gunicorn
|
||||||
|
echo "Starting Gunicorn with $WORKERS workers..."
|
||||||
exec gunicorn api.asgi:application \
|
exec gunicorn api.asgi:application \
|
||||||
--bind 0.0.0.0:8000 \
|
--bind 0.0.0.0:8000 \
|
||||||
--worker-class uvicorn.workers.UvicornWorker \
|
--worker-class uvicorn.workers.UvicornWorker \
|
||||||
--workers 4 \
|
--workers $WORKERS \
|
||||||
--threads 2
|
--threads 2 \
|
||||||
|
--timeout 120 \
|
||||||
|
--max-requests 1000 \
|
||||||
|
--max-requests-jitter 50 \
|
||||||
|
--keep-alive 5 \
|
||||||
|
--log-level error \
|
||||||
|
--capture-output \
|
||||||
|
--enable-stdio-inheritance
|
||||||
Reference in New Issue
Block a user