60 lines
1.9 KiB
Docker
Executable File
60 lines
1.9 KiB
Docker
Executable File
# 使用多阶段构建来分离构建环境和运行环境
|
||
FROM python:3.10-slim as builder
|
||
|
||
WORKDIR /app
|
||
|
||
# 1. 先只复制依赖文件
|
||
COPY requirements.txt .
|
||
|
||
# 换源和安装构建依赖
|
||
RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources \
|
||
&& sed -i 's|security.debian.org/debian-security|mirrors.ustc.edu.cn/debian-security|g' /etc/apt/sources.list.d/debian.sources
|
||
|
||
# 安装构建依赖
|
||
RUN apt-get update \
|
||
&& apt-get install -y --no-install-recommends gcc python3-dev \
|
||
&& rm -rf /var/lib/apt/lists/*
|
||
|
||
# 安装Python依赖
|
||
RUN pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple \
|
||
&& pip install --user -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple
|
||
|
||
# 第二阶段:运行环境
|
||
FROM python:3.10-slim
|
||
|
||
WORKDIR /app
|
||
|
||
# 换源
|
||
RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources \
|
||
&& sed -i 's|security.debian.org/debian-security|mirrors.ustc.edu.cn/debian-security|g' /etc/apt/sources.list.d/debian.sources
|
||
|
||
# 安装运行时的系统依赖(只安装必要的)
|
||
RUN apt-get update \
|
||
&& apt-get install -y --no-install-recommends supervisor \
|
||
&& rm -rf /var/lib/apt/lists/*
|
||
|
||
# 从构建阶段复制已安装的Python包
|
||
COPY --from=builder /root/.local /root/.local
|
||
|
||
# 设置PATH让Python可以找到用户安装的包
|
||
ENV PATH=/root/.local/bin:$PATH
|
||
ENV PYTHONPATH=/app
|
||
|
||
# 设置时区
|
||
ENV TZ="Asia/Shanghai"
|
||
|
||
# 创建目录结构
|
||
RUN mkdir -p /var/log/app_server \
|
||
&& mkdir -p /var/log/supervisor \
|
||
&& mkdir -p /etc/supervisor/conf.d
|
||
|
||
# 复制配置文件
|
||
COPY deploy/supervisor.conf /etc/supervisor/supervisord.conf
|
||
COPY deploy/app_server.conf /etc/supervisor/conf.d/
|
||
|
||
# 最后复制应用代码(利用Docker缓存层)
|
||
COPY backend/ ./backend
|
||
|
||
EXPOSE 8001
|
||
|
||
CMD ["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "8080"] |