-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
52 lines (37 loc) · 1.16 KB
/
Copy pathDockerfile
File metadata and controls
52 lines (37 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# ============ 构建阶段 ============
FROM python:3.12-slim AS builder
WORKDIR /build
# 安装构建依赖
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
curl \
&& rm -rf /var/lib/apt/lists/*
# 安装 uv (更快的包管理器)
RUN curl -LsSf https://astral.sh/uv/install.sh | sh
# 复制依赖文件
COPY requirements.txt .
# 安装依赖到虚拟环境
RUN ~/.cargo/bin/uv venv /opt/venv
RUN ~/.cargo/bin/uv pip install --python /opt/venv/bin/python -r requirements.txt
# ============ 运行阶段 ============
FROM python:3.12-slim
WORKDIR /app
# 复制虚拟环境
COPY --from=builder /opt/venv /opt/venv
# 设置环境变量
ENV PATH="/opt/venv/bin:$PATH"
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# 复制应用代码
COPY . .
# 创建数据目录
RUN mkdir -p /app/data
# 创建非 root 用户
RUN useradd -m -u 1000 appuser \
&& chown -R appuser:appuser /app
USER appuser
EXPOSE 8000
# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8000/health || exit 1
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]