Skip to content

Commit cd08e41

Browse files
committed
fix docker build
1 parent f105eb5 commit cd08e41

1 file changed

Lines changed: 60 additions & 54 deletions

File tree

Dockerfile

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,76 @@
11
# syntax=docker/dockerfile:1
22

3-
FROM alpine:3.18
3+
# 构建阶段
4+
FROM alpine:3.18 AS builder
45

5-
ENV LANG="C.UTF-8" \
6-
TZ=Asia/Shanghai \
7-
PUID=1000 \
8-
PGID=1000 \
9-
UMASK=022 \
10-
VENV_PATH="/opt/venv" \
11-
PATH="$VENV_PATH/bin:$PATH"\
12-
PATH="/root/.local/bin:$PATH"
6+
ENV VENV_PATH="/opt/venv"
137

8+
# 安装构建依赖
9+
RUN apk add --no-cache \
10+
python3 \
11+
python3-dev \
12+
gcc \
13+
musl-dev \
14+
libffi-dev \
15+
rust \
16+
cargo \
17+
curl
1418

15-
WORKDIR /app
19+
# 安装 uv
20+
RUN curl -LsSf https://astral.sh/uv/install.sh | sh
21+
ENV PATH="/root/.local/bin:$PATH"
22+
23+
# 设置工作目录
24+
WORKDIR /build
1625

17-
# Copy Python project files
26+
# 复制依赖文件并安装(这层会被缓存,除非依赖文件改变)
1827
COPY backend/pyproject.toml backend/uv.lock ./
1928

29+
# 安装Python依赖
30+
RUN uv sync --frozen --no-dev && mv .venv $VENV_PATH
31+
32+
# 运行阶段
33+
FROM alpine:3.18 AS runtime
34+
35+
ENV LANG="C.UTF-8" \
36+
TZ=Asia/Shanghai \
37+
PUID=1000 \
38+
PGID=1000 \
39+
UMASK=022 \
40+
VENV_PATH="/opt/venv" \
41+
PATH="/opt/venv/bin:/root/.local/bin:$PATH"
42+
43+
WORKDIR /app
44+
45+
# 只安装运行时依赖
2046
RUN set -ex && \
21-
apk add --no-cache \
22-
bash \
23-
busybox-suid \
24-
python3 \
25-
python3-dev \
26-
py3-aiohttp \
27-
py3-bcrypt \
28-
curl \
29-
gcc \
30-
musl-dev \
31-
libffi-dev \
32-
su-exec \
33-
shadow \
34-
tini \
35-
openssl \
36-
tzdata \
37-
rust \
38-
cargo && \
39-
# Install uv
40-
curl -LsSf https://astral.sh/uv/install.sh | sh && \
41-
# Verify uv installation and show version
42-
# Install dependencies using uv
43-
uv sync --frozen --no-dev && \
44-
# 移动 .venv 到目标位置
45-
mv .venv $VENV_PATH && \
46-
# Remove build dependencies to reduce image size
47-
apk del \
48-
rust \
49-
cargo \
50-
gcc \
51-
musl-dev \
52-
libffi-dev \
53-
python3-dev && \
54-
# Add user
55-
mkdir -p /home/ab && \
56-
addgroup -S ab -g 911 && \
57-
adduser -S ab -G ab -h /home/ab -s /sbin/nologin -u 911 && \
58-
# Clear
59-
rm -rf \
60-
/root/.cache \
61-
/root/.local \
62-
/tmp/*
47+
apk add --no-cache \
48+
bash \
49+
busybox-suid \
50+
python3 \
51+
py3-aiohttp \
52+
py3-bcrypt \
53+
curl \
54+
su-exec \
55+
shadow \
56+
tini \
57+
openssl \
58+
tzdata && \
59+
# 添加用户
60+
mkdir -p /home/ab && \
61+
addgroup -S ab -g 911 && \
62+
adduser -S ab -G ab -h /home/ab -s /sbin/nologin -u 911 && \
63+
# 清理缓存
64+
rm -rf /var/cache/apk/* /tmp/*
6365

66+
# 从构建阶段复制虚拟环境
67+
COPY --from=builder $VENV_PATH $VENV_PATH
68+
69+
# 复制应用代码(放在最后,因为代码变更最频繁)
6470
COPY --chmod=755 backend/src/. .
6571
COPY --chmod=755 entrypoint.sh /entrypoint.sh
6672

6773
ENTRYPOINT ["tini", "-g", "--", "/entrypoint.sh"]
68-
6974
EXPOSE 7892
7075
VOLUME [ "/app/config" , "/app/data" ]
76+

0 commit comments

Comments
 (0)