🚀 优化 dockerfile

This commit is contained in:
suyiiyii 2024-12-22 19:06:35 +08:00
parent 5a5b27d285
commit 93b6055391
Signed by: suyiiyii
GPG Key ID: 044704CB29B8AD85
2 changed files with 58 additions and 54 deletions

View File

@ -12,6 +12,9 @@ htmlcov
docker
dist
docs
venv
.venv
/admin-frontend/node_modules
/LICENSE
/.editorconfig
/.gitattributes

View File

@ -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"]