-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathDockerfile
More file actions
55 lines (42 loc) · 1.51 KB
/
Copy pathDockerfile
File metadata and controls
55 lines (42 loc) · 1.51 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
53
54
55
# 使用官方 Python 3.11 slim 镜像作为基础镜像
FROM python:3.11-slim
# 设置工作目录
WORKDIR /app
# 设置环境变量
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PYTHONPATH=/app/src \
TZ=Asia/Shanghai
# 更换为国内镜像源以加速
RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources
# 安装系统依赖
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
g++ \
libpq-dev \
curl \
&& rm -rf /var/lib/apt/lists/*
# 复制依赖文件并安装 Python 依赖
COPY pyproject.toml ./
COPY README.md ./
COPY src ./src
RUN pip install --no-cache-dir --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple && \
pip install --no-cache-dir . -i https://pypi.tuna.tsinghua.edu.cn/simple
# 复制项目文件
COPY . .
# 复制并设置启动脚本权限
COPY scripts/docker-entrypoint.sh /scripts/docker-entrypoint.sh
RUN chmod +x /scripts/docker-entrypoint.sh
# 创建必要的目录并切换到非 root 用户运行
RUN groupadd -r app && \
useradd -r -g app -d /app -s /sbin/nologin app && \
mkdir -p /app/logs /app/static /app/migrations && \
chown -R app:app /app /scripts
USER app
# 暴露端口
EXPOSE 8000
# 健康检查:使用业务端点(/docs 受 Basic Auth 保护,会导致误判 401)
HEALTHCHECK --interval=30s --timeout=10s --start-period=20s --retries=3 \
CMD curl -fsS http://localhost:8000/api/v1/base/health || exit 1
# 启动命令
CMD ["/scripts/docker-entrypoint.sh"]