Files

28 lines
783 B
Docker
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Dockerfile:FastAPI 应用镜像(gunicorn + uvicorn worker 多进程生产模式)
FROM python:3.10-slim
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
TZ=Asia/Shanghai
WORKDIR /app
# 先装依赖,利用 Docker 层缓存
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
# 拷贝项目代码
COPY . .
EXPOSE 8000
# gunicorn 多 worker 启动(uvicorn.workers.UvicornWorker 兼容 FastAPI 异步)
# 生产环境建议 worker 数 = CPU 核数 * 2 + 1
CMD ["gunicorn", "main:app", \
"--workers", "4", \
"--worker-class", "uvicorn.workers.UvicornWorker", \
"--bind", "0.0.0.0:8000", \
"--timeout", "60", \
"--access-logfile", "-", \
"--error-logfile", "-"]