magicdoctor_backend / Dockerfile
leonsimon23's picture
Update Dockerfile
d647faa verified
# syntax=docker/dockerfile:1.4
FROM python:3.11-slim
WORKDIR /app
# 1. 安装必要的系统工具 (git, curl 等)
RUN apt-get update && apt-get install -y --no-install-recommends \
git \
build-essential \
libpq-dev \
curl \
&& rm -rf /var/lib/apt/lists/*
# 2. 升级 pip
RUN pip install --upgrade pip
# 3. 创建并授权工作目录
RUN mkdir -p /app && chmod -R 777 /app
# 4. 使用 secret 挂载方式安全地克隆私有仓库
# - 这是最关键的一步。它会从 /run/secrets/ 中读取 GITHUB_TOKEN
# - 这种方式非常安全,token 不会出现在任何日志或中间镜像层中
RUN --mount=type=secret,id=GITHUB_TOKEN,mode=0444,required=true \
git clone https://x-access-token:$(cat /run/secrets/GITHUB_TOKEN)@github.com/leoncool23/magicdoctor_backend.git . \
|| (echo "GIT CLONE FAILED! - 请检查 GITHUB_TOKEN 是否正确并有 repo 权限。" && exit 1)
# 5. 安装 Python 依赖
RUN pip install --no-cache-dir -r requirements.txt \
|| (echo "PIP INSTALL FAILED! - 请检查 requirements.txt 文件是否正确。" && exit 1)
# 6. 设置运行时环境变量
ENV FLASK_APP=app/main.py
ENV FLASK_ENV=production
ENV PYTHONPATH=/app
ENV PORT=7860
# 7. 暴露端口
EXPOSE 7860
# 8. 启动命令
#CMD ["sh", "-c", "python app/init_db.py && gunicorn --workers 4 --worker-class gevent --timeout 300 --bind 0.0.0.0:7860 'app.main:app'"]
CMD ["sh", "-c", "python -m app.init_db && gunicorn --workers 4 --bind 0.0.0.0:7860 'app.main:create_app()'"]