From 93b6055391b9beba93ebc0523fc59f8c412752fd Mon Sep 17 00:00:00 2001
From: suyiiyii <suyiiyii@gmail.com>
Date: Sun, 22 Dec 2024 19:06:35 +0800
Subject: [PATCH] =?UTF-8?q?:rocket:=20=E4=BC=98=E5=8C=96=20dockerfile?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .dockerignore                   |   3 +
 docker/Dockerfile_with_frontend | 109 ++++++++++++++++----------------
 2 files changed, 58 insertions(+), 54 deletions(-)

diff --git a/.dockerignore b/.dockerignore
index 4db2154..85a98a5 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -12,6 +12,9 @@ htmlcov
 docker
 dist
 docs
+venv
+.venv
+/admin-frontend/node_modules
 /LICENSE
 /.editorconfig
 /.gitattributes
diff --git a/docker/Dockerfile_with_frontend b/docker/Dockerfile_with_frontend
index 60da38e..40987e8 100644
--- a/docker/Dockerfile_with_frontend
+++ b/docker/Dockerfile_with_frontend
@@ -79,6 +79,21 @@ RUN echo "πŸ—οΈ Starting frontend build..." && \
     pnpm run build && \
     echo "βœ… Frontend build completed"
 
+FROM python:3.12-slim AS playwright-stage
+
+WORKDIR /tmp
+
+ENV DEBIAN_FRONTEND=noninteractive \
+    PYTHONUNBUFFERED=1 \
+    TZ=Asia/Shanghai
+
+COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
+
+
+RUN echo "πŸ“¦ Installing playwright..." && \
+    uvx playwright install --with-deps chromium && \
+    echo "βœ… Playwright installed successfully"
+
 FROM python:3.12-slim AS production
 
 WORKDIR /app
@@ -107,29 +122,37 @@ RUN echo "πŸ“¦ Installing system dependencies..." && \
         fonts-liberation \
         fonts-ipafont-gothic \
         fonts-wqy-zenhei \
-        fonts-tlwg-loma-otf && \
-    mkdir -p /etc/apt/keyrings && \
-    curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
-    apt-get install -y nodejs && \
-    echo "πŸ”” Verifying Node.js installation..." && \
-    if NODE_VERSION=$(node --version 2>&1); then \
-        echo "βœ… Node.js version ${NODE_VERSION} installed successfully"; \
-    else \
-        echo "❌ Node.js installation failed" && exit 1; \
-    fi && \
-    mkdir -p "${PNPM_HOME}" && \
-    npm install -g pnpm@9.15.1 && \
-    # ι’„δΈ‹θ½½ pnpm εŒ…εˆ°ε…¨ε±€ε­˜ε‚¨
-    curl -fsSL https://registry.npmjs.org/pnpm/-/pnpm-9.15.1.tgz -o /tmp/pnpm.tgz && \
-    mkdir -p /root/.local/share/pnpm/store/v3 && \
-    pnpm store add /tmp/pnpm.tgz && \
-    rm /tmp/pnpm.tgz && \
-    echo "πŸ”” Verifying pnpm installation..." && \
-    if PNPM_VERSION=$(pnpm --version 2>&1); then \
-        echo "βœ… pnpm version ${PNPM_VERSION} installed successfully"; \
-    else \
-        echo "❌ pnpm installation failed" && exit 1; \
-    fi && \
+        fonts-tlwg-loma-otf \
+        at-spi2-common \
+        fonts-freefont-ttf \
+        libasound2 \
+        libasound2-data \
+        libatk-bridge2.0-0 \
+        libatk1.0-0 \
+        libatspi2.0-0 \
+        libavahi-client3 \
+        libavahi-common-data \
+        libavahi-common3 \
+        libcairo2 \
+        libcups2 \
+        libdatrie1 \
+        libdbus-1-3 \
+        libfribidi0 \
+        libgbm1 \
+        libglib2.0-0 \
+        libgraphite2-3 \
+        libharfbuzz0b \
+        libnspr4 \
+        libnss3 \
+        libpango-1.0-0 \
+        libthai-data \
+        libthai0 \
+        libwayland-server0 \
+        libxcb-render0 \
+        libxcomposite1 \
+        libxdamage1 \
+        libxi6 \
+        libxkbcommon0 && \
     fc-cache -fv && \
     apt-get clean && \
     rm -rf /var/lib/apt/lists/* && \
@@ -146,48 +169,26 @@ RUN echo "πŸ”” Verifying uv installation..." && \
 
 COPY ./pyproject.toml ./uv.lock ./
 
-RUN echo "πŸ“¦ Installing bison dependencies..." && \
+RUN --mount=type=cache,target=/root/.cache/ \
+    --mount=type=cache,target=/root/.uv \
+    echo "πŸ“¦ Installing bison dependencies..." && \
     uv pip install --extra=docker --requirement pyproject.toml --no-cache && \
     echo "βœ… Dependencies installed successfully" && \
     echo "πŸ”” Cleaning up uv..." && \
     rm -rf /bin/uv /bin/uvx && \
-    rm -rf /root/.cache/ && \
-    rm -rf /root/.uv && \
     echo "βœ… UV cleanup completed"
 
 COPY --from=metadata-stage /tmp/VERSION /app/VERSION
-COPY . .
-COPY --from=frontend-stage /tmp/nonebot_bison/admin_page/dist ./dist
-COPY --from=frontend-stage /tmp/admin-frontend/package.json ./
-COPY --from=frontend-stage /tmp/admin-frontend/pnpm-lock.yaml ./
+COPY nonebot_bison/ /app/nonebot_bison/
+COPY --from=frontend-stage /tmp/nonebot_bison/admin_page/dist ./nonebot_bison/admin_page/dist
+COPY --from=playwright-stage /root/.cache/ms-playwright /root/.cache/ms-playwright
 
-
-#RUN echo "πŸ“¦ Installing frontend dependencies..." && \
-#    cd /app/admin-frontend && \
-#    SHELL="/bin/bash" pnpm install --offline --prod --frozen-lockfile && \
-#    rm -rf /root/.cache/ && \
-#    echo "βœ… Frontend dependencies installed successfully"
-RUN echo "πŸ“¦ Installing frontend dependencies..." && \
-    cd /app/admin-frontend && \
-    SHELL="/bin/bash" pnpm install --prod --frozen-lockfile --no-verify-store-integrity && \
-    rm -rf /root/.cache/ /root/.local/share/pnpm/store/v3/* /root/.pnpm-store && \
-    echo "βœ… Frontend dependencies installed successfully"
-
-
-RUN echo "🎭 Installing Playwright..." && \
-    playwright install --with-deps chromium && \
-    rm -rf /var/lib/apt/lists/* /tmp/* && \
-    mv docker.env.prod .env.prod && \
-    echo "βœ… Playwright installed successfully"
-
-EXPOSE 3000 8080
+EXPOSE 8080
 
 RUN echo '#!/bin/bash' > /app/start.sh && \
     echo 'echo "πŸ“Œ Current bison Version: $(cat /app/VERSION)"' >> /app/start.sh && \
-    echo 'echo "πŸš€ Starting backend service..."' >> /app/start.sh && \
-    echo 'nb run &' >> /app/start.sh && \
-    echo 'echo "πŸš€ Starting frontend service..."' >> /app/start.sh && \
-    echo 'cd /app/admin-frontend && SHELL=/bin/bash exec pnpm start' >> /app/start.sh && \
+    echo 'echo "πŸš€ Starting service..."' >> /app/start.sh && \
+    echo 'nb run' >> /app/start.sh && \
     chmod +x /app/start.sh
 
 CMD ["/app/start.sh"]