# 使用Python 3.9作为基础镜像以匹配开发环境 FROM python:3.9-slim # 设置工作目录 WORKDIR /app # 设置环境变量 ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ FLASK_APP=run.py \ FLASK_ENV=production # 创建并配置国内镜像源 RUN echo "deb https://mirrors.ustc.edu.cn/debian/ bookworm main" > /etc/apt/sources.list && \ echo "deb https://mirrors.ustc.edu.cn/debian/ bookworm-updates main" >> /etc/apt/sources.list && \ echo "deb https://mirrors.ustc.edu.cn/debian-security/ bookworm-security main" >> /etc/apt/sources.list # 安装系统依赖(包括libmagic) RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ default-libmysqlclient-dev \ libmagic1 \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* # 使用阿里云pip镜像源(更稳定) RUN pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ && \ pip config set install.trusted-host mirrors.aliyun.com # 安装Python依赖 COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # 复制项目文件 COPY . . # 运行配置更新脚本 RUN python update_config.py # 创建非root用户运行应用 RUN useradd -m appuser RUN chown -R appuser:appuser /app USER appuser # 暴露自定义端口50400 EXPOSE 50400 # 启动命令 CMD ["gunicorn", "--bind", "0.0.0.0:50400", "run:app"]