-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDockerfile
More file actions
73 lines (56 loc) · 2.15 KB
/
Copy pathDockerfile
File metadata and controls
73 lines (56 loc) · 2.15 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
# syntax=docker/dockerfile:1
# 多阶段构建:编译阶段。固定多架构索引,确保相同提交使用相同基础镜像。
FROM python:3.14-alpine@sha256:05b2b8b732ecd268fee8727a369f936f022d1321b59befd13c30ede22769dcdc 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
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 .
# Upgrade packaging tools first, then build a deterministic wheelhouse.
RUN pip install --no-cache-dir --upgrade pip setuptools wheel \
&& pip wheel --no-cache-dir --wheel-dir /wheels -r requirements.txt
# 运行阶段:使用与构建阶段相同的最小化镜像
FROM python:3.14-alpine@sha256:05b2b8b732ecd268fee8727a369f936f022d1321b59befd13c30ede22769dcdc
# 设置运行时环境变量
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV PIP_NO_CACHE_DIR=1
ENV TZ=Asia/Shanghai
# 固定的 Python Alpine 基础镜像已包含 CA 证书与时区数据,仅配置运行时区。
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone
WORKDIR /app
# 使用非 root 用户运行(安全考虑)
RUN addgroup -g 1000 appuser && \
adduser -D -s /bin/sh -u 1000 -G appuser appuser && \
mkdir -p /app/data && \
chown appuser:appuser /app/data
# 只读挂载构建阶段的 wheel,不把临时 wheelhouse 写入运行镜像层。
RUN --mount=type=bind,from=builder,source=/wheels,target=/wheels \
pip install --no-cache-dir /wheels/*
# 复制应用代码
COPY --chown=appuser:appuser src/ ./src/
USER appuser
# Documentation only; listeners remain disabled unless explicitly configured.
EXPOSE 8765 7654
HEALTHCHECK --interval=30s --timeout=5s --start-period=90s --retries=3 \
CMD ["python", "-m", "src.healthcheck"]
CMD ["python", "-m", "src.main"]