-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDockerfile
More file actions
85 lines (69 loc) · 2.23 KB
/
Dockerfile
File metadata and controls
85 lines (69 loc) · 2.23 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 多阶段构建:编译阶段
FROM python:3.14-alpine AS builder
# 设置构建参数
ARG BUILDKIT_INLINE_CACHE=1
# 安装编译依赖(包括 Rust 编译器)
RUN apk add --no-cache \
gcc \
g++ \
make \
libffi-dev \
libsodium-dev \
musl-dev \
python3-dev \
rust \
cargo \
openssl-dev \
pkgconfig
# 设置 Rust 编译优化
ENV RUSTFLAGS="-C target-cpu=native"
ENV CARGO_NET_GIT_FETCH_WITH_CLI=true
ENV CARGO_BUILD_JOBS=4
ENV OPENSSL_DIR=/usr
ENV OPENSSL_LIBDIR=/usr/lib
ENV PKG_CONFIG_PATH=/usr/lib/pkgconfig
ENV PKG_CONFIG_LIBDIR=/usr/lib/pkgconfig
# 复制依赖文件
COPY requirements.txt .
# 预编译所有包为wheel格式,使用并行编译
RUN pip wheel --no-cache-dir --wheel-dir /wheels -r requirements.txt \
&& pip install --no-cache-dir --upgrade pip setuptools wheel \
|| (echo "Wheel build failed, trying with pre-built packages..." && \
pip install --no-cache-dir --only-binary=all -r requirements.txt && \
pip wheel --no-cache-dir --wheel-dir /wheels --only-binary=all -r requirements.txt)
# 运行阶段:使用最小化镜像
FROM python:3.14-alpine
# 设置运行时环境变量
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV PIP_NO_CACHE_DIR=1
ENV TZ=Asia/Shanghai
# 安装运行时依赖(最小化)
RUN apk add --no-cache \
curl \
ca-certificates \
tzdata \
&& rm -rf /var/cache/apk/* \
&& update-ca-certificates \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone
WORKDIR /app
# 从编译阶段复制预编译的wheel包
COPY --from=builder /wheels /wheels
# 安装预编译的包(避免编译)
RUN pip install --no-cache-dir /wheels/* && rm -rf /wheels
# 复制应用代码
COPY src/ ./src/
COPY start.sh .
RUN chmod +x start.sh
# 使用非root用户运行(安全考虑)
RUN addgroup -g 1000 appuser && \
adduser -D -s /bin/sh -u 1000 -G appuser appuser && \
mkdir -p /app/data && \
chown -R appuser:appuser /app
USER appuser
# 健康检查(暂时禁用,因为应用可能没有健康检查端点)
# HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
# CMD curl -f http://localhost:8080/health || exit 1
EXPOSE 8080
CMD ["./start.sh"]