Files
html2pdf-server/Dockerfile
2025-10-20 15:18:52 +08:00

80 lines
1.9 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# ===============================
# 极致最小化分步构建 Node 18 + Playwright
# ===============================
# -------------------------------
# Stage 1: 构建层安装依赖、Chromium
# -------------------------------
FROM node:18-slim AS builder
WORKDIR /app
# 复制 package.json / package-lock.json
COPY package*.json ./
# 安装生产依赖(包含 Playwright
RUN npm install --omit=dev && npm cache clean --force
# 复制自定义中文字体并刷新缓存
COPY ./fonts/* /usr/share/fonts/
RUN apt update && \
apt install -y --no-install-recommends fontconfig && \
fc-cache -fv && \
rm -rf /var/lib/apt/lists/*
# 安装 Playwright Chromium headless
RUN npx playwright install chromium --only-shell
# 复制应用代码
COPY server.js ./
# -------------------------------
# Stage 2: 运行层(最终镜像)
# -------------------------------
FROM node:18-slim AS runtime
WORKDIR /app
# 安装依赖其中lib开头的是 playwright 需要的依赖。不同版本可能不一样
RUN apt update && \
apt install -y --no-install-recommends \
fontconfig \
libglib2.0-0 \
libnspr4 \
libnss3 \
libdbus-1-3 \
libatk1.0-0 \
libatk-bridge2.0-0 \
libatspi2.0-0 \
libx11-6 \
libxcomposite1 \
libxdamage1 \
libxext6 \
libxfixes3 \
libxrandr2 \
libgbm1 \
libxcb1 \
libxkbcommon0 \
libasound2 && \
rm -rf /var/lib/apt/lists/*
# 复制字体
COPY --from=builder /usr/share/fonts/ /usr/share/fonts/
RUN fc-cache -fv
# 复制 package.json 和 node_modules包含 Playwright 及依赖)
COPY --from=builder /app/package.json /app/package.json
COPY --from=builder /app/node_modules /app/node_modules
# 复制应用代码
COPY --from=builder /app/server.js ./
# 复制 Playwright Chromium 缓存
COPY --from=builder /root/.cache/ms-playwright /root/.cache/ms-playwright
# 暴露端口
EXPOSE 3000
# 启动应用
CMD ["npm", "start"]