update
This commit is contained in:
parent
04c0278e0c
commit
33bbea3406
97
Dockerfile
97
Dockerfile
@ -1,20 +1,13 @@
|
||||
# 构建阶段:安装依赖和编译
|
||||
FROM python:3.9-slim AS builder
|
||||
|
||||
# 设置工作目录
|
||||
WORKDIR /build
|
||||
|
||||
# 设置环境变量
|
||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||
PYTHONUNBUFFERED=1 \
|
||||
PYTHONIOENCODING=utf-8 \
|
||||
TZ=Asia/Shanghai
|
||||
# 使用Python 3.10作为基础镜像
|
||||
FROM python:3.10-slim
|
||||
|
||||
# 设置时区
|
||||
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
|
||||
echo "Asia/Shanghai" > /etc/timezone
|
||||
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
|
||||
# 清空所有默认源
|
||||
RUN rm -rf /etc/apt/sources.list.d/* && \
|
||||
rm -f /etc/apt/sources.list
|
||||
|
||||
# 使用阿里云镜像源
|
||||
# 替换为阿里云源
|
||||
RUN echo "\
|
||||
deb https://mirrors.aliyun.com/debian/ bookworm main non-free-firmware contrib\n\
|
||||
deb-src https://mirrors.aliyun.com/debian/ bookworm main non-free-firmware contrib\n\
|
||||
@ -26,75 +19,27 @@ RUN echo "\
|
||||
deb-src https://mirrors.aliyun.com/debian-security bookworm-security main non-free-firmware contrib\n\
|
||||
" > /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 \
|
||||
# 安装系统依赖
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends \
|
||||
build-essential \
|
||||
default-libmysqlclient-dev \
|
||||
pkg-config \
|
||||
&& 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
|
||||
# 复制项目文件
|
||||
COPY requirements.txt .
|
||||
COPY app app/
|
||||
|
||||
# 设置时区
|
||||
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
|
||||
echo "Asia/Shanghai" > /etc/timezone
|
||||
|
||||
# 使用阿里云镜像源
|
||||
RUN mkdir -p /etc/apt/sources.list.d && \
|
||||
echo "deb https://mirrors.aliyun.com/debian/ bookworm main non-free contrib" > /etc/apt/sources.list.d/aliyun.list && \
|
||||
echo "deb https://mirrors.aliyun.com/debian-security/ bookworm-security main" >> /etc/apt/sources.list.d/aliyun.list && \
|
||||
echo "deb https://mirrors.aliyun.com/debian/ bookworm-updates main non-free contrib" >> /etc/apt/sources.list.d/aliyun.list && \
|
||||
echo "deb https://mirrors.aliyun.com/debian/ bookworm-backports main non-free contrib" >> /etc/apt/sources.list.d/aliyun.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
|
||||
# 安装Python依赖
|
||||
RUN pip install -i https://mirrors.aliyun.com/pypi/simple/ -r requirements.txt \
|
||||
&& pip install uvicorn
|
||||
|
||||
# 暴露端口
|
||||
EXPOSE 9001
|
||||
|
||||
# 设置健康检查
|
||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
|
||||
CMD curl -f http://localhost:9001/health || exit 1
|
||||
EXPOSE 8000
|
||||
|
||||
# 启动命令
|
||||
CMD ["uvicorn", "run:app", "--host", "0.0.0.0", "--port", "9001"]
|
||||
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||
Loading…
Reference in New Issue
Block a user