93 lines
3.0 KiB
Docker
93 lines
3.0 KiB
Docker
# 构建阶段:安装依赖和编译
|
|
FROM python:3.9-slim AS builder
|
|
|
|
# 设置工作目录
|
|
WORKDIR /build
|
|
|
|
# 设置环境变量
|
|
ENV PYTHONDONTWRITEBYTECODE=1 \
|
|
PYTHONUNBUFFERED=1 \
|
|
PYTHONIOENCODING=utf-8 \
|
|
TZ=Asia/Shanghai
|
|
|
|
# 设置时区
|
|
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
|
|
echo "Asia/Shanghai" > /etc/timezone
|
|
|
|
# 使用阿里云镜像源
|
|
RUN echo "deb https://mirrors.aliyun.com/debian/ bullseye main non-free contrib" > /etc/apt/sources.list && \
|
|
echo "deb https://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list && \
|
|
echo "deb https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list && \
|
|
echo "deb https://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >> /etc/apt/sources.list
|
|
|
|
# 安装构建依赖
|
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
build-essential \
|
|
libssl-dev \
|
|
libffi-dev \
|
|
default-libmysqlclient-dev \
|
|
pkg-config \
|
|
&& 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 \
|
|
&& pip install --no-cache-dir --upgrade pip setuptools wheel
|
|
|
|
# 复制requirements.txt
|
|
COPY requirements.txt .
|
|
|
|
# 安装Python依赖
|
|
RUN pip install --no-cache-dir -r requirements.txt
|
|
|
|
# 最终阶段:创建运行镜像
|
|
FROM python:3.9-slim
|
|
|
|
# 设置工作目录
|
|
WORKDIR /app
|
|
|
|
# 设置环境变量
|
|
ENV PYTHONDONTWRITEBYTECODE=1 \
|
|
PYTHONUNBUFFERED=1 \
|
|
PYTHONIOENCODING=utf-8 \
|
|
TZ=Asia/Shanghai
|
|
|
|
# 设置时区
|
|
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
|
|
echo "Asia/Shanghai" > /etc/timezone
|
|
|
|
# 使用阿里云镜像源
|
|
RUN echo "deb https://mirrors.aliyun.com/debian/ bullseye main non-free contrib" > /etc/apt/sources.list && \
|
|
echo "deb https://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list && \
|
|
echo "deb https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list && \
|
|
echo "deb https://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >> /etc/apt/sources.list
|
|
|
|
# 安装运行时依赖
|
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
default-libmysqlclient-dev \
|
|
curl \
|
|
&& apt-get clean \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
# 从构建阶段复制Python包
|
|
COPY --from=builder /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages
|
|
COPY --from=builder /usr/local/bin /usr/local/bin
|
|
|
|
# 复制应用代码
|
|
COPY . /app/
|
|
|
|
# 创建非root用户运行应用
|
|
RUN adduser --disabled-password --gecos '' appuser
|
|
RUN chown -R appuser:appuser /app
|
|
USER appuser
|
|
|
|
# 暴露端口
|
|
EXPOSE 9001
|
|
|
|
# 设置健康检查
|
|
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
|
|
CMD curl -f http://localhost:9001/health || exit 1
|
|
|
|
# 启动命令
|
|
CMD ["uvicorn", "run:app", "--host", "0.0.0.0", "--port", "9001"] |