From 9f9c5f814f52693e9a7c18d896dc0bb137e6f047 Mon Sep 17 00:00:00 2001 From: aifeilong <2993607249@qq.com> Date: Wed, 14 Oct 2026 17:45:23 +0800 Subject: [PATCH] Add Docker packaging for production deployment with uWSGI. Co-authored-by: Cursor --- Dockerfile | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ entrypoint.sh | 3 +++ uwsgi.prod.ini | 16 +++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 Dockerfile create mode 100755 entrypoint.sh create mode 100644 uwsgi.prod.ini diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e3aab85 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,54 @@ +# 使用官方的Python基础镜像 +FROM python:3.10-slim + +# 替换 sources.list 中的源为 USTC 镜像 +# RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources +RUN sed -i "s@http://deb.debian.org/debian@http://mirrors.tencent.com/debian@g" /etc/apt/sources.list.d/debian.sources + +RUN apt-get update && apt-get install -y \ + build-essential \ + git \ + gcc \ + autoconf \ + automake \ + pkg-config \ + libtool \ + locales + +# 生成 en_US.UTF-8 locale +RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && \ + locale-gen en_US.UTF-8 + +# Set environment variables +ENV LANG=en_US.UTF-8 +ENV LANGUAGE=en_US:en +ENV LC_ALL=en_US.UTF-8 +# Verify that the locale is correctly set (optional) +RUN locale + +RUN pip config set global.index-url https://mirrors.ustc.edu.cn/pypi/simple +RUN pip install --upgrade pip + +# 设置工作目录 +WORKDIR /app + +# 复制依赖文件并安装依赖 +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +# 安装uWSGI +RUN pip install uwsgi + +# 复制项目文件 +COPY . . + +# 暴露uWSGI的默认端口 +EXPOSE 8000 + +COPY ./entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh + +# 使用uWSGI运行Django应用 +CMD ["uwsgi", "--ini", "uwsgi.prod.ini"] + +ENTRYPOINT ["/entrypoint.sh"] diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..9701bbd --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,3 @@ +#!/bin/sh +./manage.py migrate +exec "$@" diff --git a/uwsgi.prod.ini b/uwsgi.prod.ini new file mode 100644 index 0000000..654a508 --- /dev/null +++ b/uwsgi.prod.ini @@ -0,0 +1,16 @@ +[uwsgi] +env = LANG=en_US.UTF-8 +env = LC_ALL=en_US.UTF-8 +http = :8000 +chdir = /app +wsgi_file = config/wsgi.py +module = config.wsgi:application +master = True +socket = /tmp/uwsgi.sock +chmod-socket = 666 +vacuum = true +workers = 2 +threads = 4 +http-timeout = 1800 # 或更大的值 +socket-timeout = 1800 +harakiri = 1800 # 进程超时时间(秒),建议是API最大可能执行时间的2倍