Skip to content

Commit 071f2dc

Browse files
committed
build(Dockerfile): 优化 Docker 构建流程并增强 Playwright 安装稳定性
- 添加系统依赖安装步骤,确保基础环境完整 - 分离依赖安装与源码复制步骤,提升构建可读性 - 跳过 npm postinstall 脚本以避免早期依赖冲突 - 单独运行 prebuild 和 build 脚本以提高构建可控性 - 为 Playwright 安装增加重试机制(最多 3 次),提升 CI 稳定性 - 清理不必要的缓存和文件,减小镜像体积
1 parent f3bf0d5 commit 071f2dc

1 file changed

Lines changed: 38 additions & 9 deletions

File tree

Dockerfile

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,45 @@
11
FROM node:lts-bookworm-slim
22

3+
# 安装必要的系统依赖
4+
RUN apt-get update && apt-get install -y \
5+
curl \
6+
wget \
7+
gnupg \
8+
&& rm -rf /var/lib/apt/lists/*
9+
310
WORKDIR /app
411

12+
# 复制 package 文件
13+
COPY package*.json ./
14+
COPY tsconfig.json ./
15+
16+
# 安装所有依赖(包括开发依赖,用于构建)
17+
# 跳过 postinstall 脚本(避免过早安装 Playwright)
18+
RUN npm ci --ignore-scripts
19+
20+
# 复制源代码
521
COPY . .
622

7-
RUN npm install \
8-
&& npm run build \
9-
&& npx playwright install --with-deps chromium \
10-
&& apt-get clean \
11-
&& npm prune --omit=dev \
12-
&& npm cache clean --force \
13-
&& rm -rf /var/lib/apt/lists/* \
14-
&& rm -rf /var/cache/apt/*
23+
# 单独构建项目(不运行 prepare 脚本)
24+
RUN npm run prebuild && npm run build
25+
26+
# 安装 Playwright,增加重试和更好的错误处理
27+
RUN for i in 1 2 3; do \
28+
echo "Attempt $i: Installing Playwright..." && \
29+
npx playwright install --with-deps chromium && break || \
30+
if [ $i -eq 3 ]; then \
31+
echo "Failed to install Playwright after 3 attempts" && exit 1; \
32+
fi && \
33+
echo "Attempt $i failed, retrying in 30 seconds..." && \
34+
sleep 30; \
35+
done
36+
37+
# 清理缓存和不需要的文件
38+
RUN npm prune --omit=dev && \
39+
apt-get clean && \
40+
npm cache clean --force && \
41+
rm -rf /var/lib/apt/lists/* && \
42+
rm -rf /var/cache/apt/* && \
43+
rm -rf /tmp/*
1544

16-
CMD ["node", "build/index.js"]
45+
CMD ["node", "build/index.js"]

0 commit comments

Comments
 (0)