chore: 优化 Docker 配置,添加生产环境部署脚本和文档

This commit is contained in:
爽哒哒
2026-03-18 22:48:36 +08:00
parent f655aa0ede
commit e3a620b395
8 changed files with 610 additions and 43 deletions

View File

@@ -1,26 +1,40 @@
# Use an official Python runtime as a parent image
FROM python:3.13-slim
# 使用 Python 3.12 slim 镜像作为基础镜像
FROM python:3.12-slim
# Set environment variables
# 设置环境变量
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV PYTHONIOENCODING=utf-8
# Set work directory
# 设置工作目录
WORKDIR /app
# Install python dependencies
COPY requirements.txt /app/
RUN pip install --upgrade pip && pip install -r requirements.txt
# 安装系统依赖
RUN apt-get update && apt-get install -y \
gcc \
libpq-dev \
libffi-dev \
libjpeg-dev \
zlib1g-dev \
&& rm -rf /var/lib/apt/lists/*
# Copy project
COPY . /app/
COPY .env /app/
# 复制 requirements 文件并安装依赖
COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir -r requirements.txt
# Expose port
# 复制项目代码
COPY . .
# 创建媒体文件目录
RUN mkdir -p /app/media /app/static
# 暴露端口
EXPOSE 8000
# Volume for media files
VOLUME ["/app/media"]
# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD python -c "import requests; requests.get('http://localhost:8000/api/health/')" || exit 1
# Run the application with gunicorn
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "config.wsgi:application"]
# 启动命令
CMD ["sh", "-c", "python manage.py collectstatic --noinput && python manage.py migrate && gunicorn --bind 0.0.0.0:8000 --workers 4 --threads 2 --worker-class gthread --access-logfile - --error-logfile - --capture-output --enable-stdio-inheritance config.wsgi:application"]