Skip to content

Commit 1c37db7

Browse files
committed
feat: optimize Docker build process
1 parent bbfd14a commit 1c37db7

3 files changed

Lines changed: 430 additions & 64 deletions

File tree

Dockerfile

Lines changed: 69 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,95 @@
1-
# syntax=docker/dockerfile:1
2-
3-
FROM alpine:3.18
1+
# Multi-stage Dockerfile for Auto_Bangumi
2+
# Stage 1: Python base runtime
3+
FROM alpine:3.18 AS python-base
44

55
ENV LANG="C.UTF-8" \
66
TZ=Asia/Shanghai \
77
PUID=1000 \
88
PGID=1000 \
99
UMASK=022 \
10-
PATH="/root/.local/bin:$PATH"
10+
VENV_PATH="/opt/venv"
11+
12+
# Install runtime system packages
13+
RUN set -ex && \
14+
apk add --no-cache \
15+
bash \
16+
busybox-suid \
17+
python3 \
18+
py3-aiohttp \
19+
py3-bcrypt \
20+
curl \
21+
su-exec \
22+
shadow \
23+
tini \
24+
openssl \
25+
tzdata && \
26+
# Set timezone
27+
ln -sf /usr/share/zoneinfo/$TZ /etc/localtime && \
28+
echo $TZ > /etc/timezone
29+
30+
# Stage 2: Build dependencies
31+
FROM python-base AS build-deps
32+
33+
# Install build tools and dependencies
34+
RUN set -ex && \
35+
apk add --no-cache \
36+
build-base \
37+
gcc \
38+
musl-dev \
39+
libffi-dev \
40+
python3-dev \
41+
rust \
42+
cargo
43+
44+
# Install uv package manager
45+
RUN curl -LsSf https://astral.sh/uv/install.sh | sh && \
46+
mv /root/.cargo/bin/uv /usr/local/bin/uv && \
47+
uv --version
48+
49+
# Stage 3: Python virtual environment
50+
FROM build-deps AS python-venv
51+
52+
ENV PATH="${VENV_PATH}/bin:${PATH}"
1153

1254
WORKDIR /app
1355

1456
# Copy Python project files
1557
COPY backend/pyproject.toml backend/uv.lock ./
1658

59+
# Create virtual environment and install dependencies
1760
RUN set -ex && \
18-
apk add --no-cache \
19-
bash \
20-
busybox-suid \
21-
python3 \
22-
python3-dev \
23-
py3-aiohttp \
24-
py3-bcrypt \
25-
curl \
26-
gcc \
27-
musl-dev \
28-
libffi-dev \
29-
su-exec \
30-
shadow \
31-
tini \
32-
openssl \
33-
tzdata \
34-
rust \
35-
cargo && \
36-
# Install uv
37-
curl -LsSf https://astral.sh/uv/install.sh | sh && \
38-
# Verify uv installation and show version
39-
uv --version && \
4061
# Check if lock file exists and is readable
4162
ls -la uv.lock && \
63+
# Create virtual environment
64+
python3 -m venv ${VENV_PATH} && \
4265
# Install dependencies using uv
4366
uv sync --frozen --no-dev && \
44-
# Remove build dependencies to reduce image size
45-
apk del \
46-
rust \
47-
cargo \
48-
gcc \
49-
musl-dev \
50-
libffi-dev \
51-
python3-dev && \
52-
# Add user
67+
# Clear cache
68+
rm -rf /root/.cache
69+
70+
# Stage 4: Final runtime image
71+
FROM python-base AS final
72+
73+
ENV PATH="/root/.local/bin:${VENV_PATH}/bin:${PATH}"
74+
75+
WORKDIR /app
76+
77+
# Copy virtual environment from build stage
78+
COPY --from=python-venv --chown=root:root ${VENV_PATH} ${VENV_PATH}
79+
80+
# Add user
81+
RUN set -ex && \
5382
mkdir -p /home/ab && \
5483
addgroup -S ab -g 911 && \
55-
adduser -S ab -G ab -h /home/ab -s /sbin/nologin -u 911 && \
56-
# Clear
57-
rm -rf \
58-
/root/.cache \
59-
/root/.local \
60-
/tmp/*
84+
adduser -S ab -G ab -h /home/ab -s /sbin/nologin -u 911
6185

86+
# Copy application code and scripts
6287
COPY --chmod=755 backend/src/. .
6388
COPY --chmod=755 entrypoint.sh /entrypoint.sh
6489

90+
# Set proper permissions
91+
RUN chown -R ab:ab /app /home/ab
92+
6593
ENTRYPOINT ["tini", "-g", "--", "/entrypoint.sh"]
6694

6795
EXPOSE 7892

0 commit comments

Comments
 (0)