Skip to content

Commit 64774e3

Browse files
authored
Update Dockerfile
1 parent aa940cb commit 64774e3

1 file changed

Lines changed: 46 additions & 7 deletions

File tree

Dockerfile

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ FROM node:18-alpine AS base
44
# 设置工作目录
55
WORKDIR /app
66

7-
# 配置国内镜像源
8-
RUN npm config set registry https://registry.npmmirror.com/
7+
# # 配置国内镜像源
8+
# RUN npm config set registry https://registry.npmmirror.com/
99

1010
# 安装必要的系统依赖(例如 CA 证书)
11-
RUN apk add --no-cache ca-certificates
11+
RUN apk add --no-cache ca-certificates && update-ca-certificates
1212

1313
# 阶段2:构建应用程序
1414
FROM base AS builder
@@ -18,8 +18,29 @@ WORKDIR /app
1818
# 复制依赖文件
1919
COPY package.json yarn.lock ./
2020

21-
# 安装所有依赖,包括开发依赖
22-
RUN yarn install
21+
# 配置镜像源
22+
ARG REGISTRY_LIST="https://registry.npmmirror.com/ https://registry.npmjs.org/ https://registry.yarnpkg.com/"
23+
ARG YARN_NETWORK_TIMEOUT="300000"
24+
25+
# 多镜像源回退安装:开发依赖 + 构建所需
26+
RUN set -eux; \
27+
success=0; \
28+
for registry in $REGISTRY_LIST; do \
29+
echo "尝试 Yarn 镜像源: $registry"; \
30+
yarn config set registry "$registry"; \
31+
if yarn install --frozen-lockfile --network-timeout "$YARN_NETWORK_TIMEOUT"; then \
32+
echo "成功使用镜像源: $registry"; \
33+
success=1; \
34+
break; \
35+
else \
36+
echo "镜像源 $registry 失败,清缓存并尝试下一个..."; \
37+
yarn cache clean || true; \
38+
fi; \
39+
done; \
40+
if [ "$success" -ne 1 ]; then echo "所有镜像源尝试失败"; exit 1; fi
41+
42+
# # 安装所有依赖,包括开发依赖
43+
# RUN yarn install
2344

2445
# 复制项目源代码
2546
COPY . .
@@ -34,17 +55,35 @@ RUN rm -rf node_modules
3455
ENV NODE_ENV=production
3556

3657
# 安装生产依赖
37-
RUN yarn install --production --ignore-scripts --prefer-offline
58+
# RUN yarn install --production --ignore-scripts --prefer-offline
59+
RUN set -eux; \
60+
success=0; \
61+
for registry in $REGISTRY_LIST; do \
62+
echo "尝试 Yarn 镜像源(生产依赖): $registry"; \
63+
yarn config set registry "$registry"; \
64+
if yarn install --frozen-lockfile --production --ignore-scripts --network-timeout "$YARN_NETWORK_TIMEOUT"; then \
65+
echo "成功使用镜像源(生产依赖): $registry"; \
66+
success=1; \
67+
break; \
68+
else \
69+
echo "镜像源 $registry 失败,清缓存并尝试下一个..."; \
70+
yarn cache clean || true; \
71+
fi; \
72+
done; \
73+
if [ "$success" -ne 1 ]; then echo "所有镜像源尝试失败"; exit 1; fi
3874

3975
# 清理 yarn 缓存
40-
RUN yarn cache clean --all
76+
RUN yarn cache clean --all || true
4177

4278
# 阶段3:构建最终的生产镜像
4379
FROM node:18-alpine
4480

4581
# 设置工作目录
4682
WORKDIR /app
4783

84+
# 运行时同样装 CA 证书(以防某些运行时场景需要出网)
85+
RUN apk add --no-cache ca-certificates && update-ca-certificates
86+
4887
# 创建非 root 用户
4988
RUN addgroup -g 1001 appgroup && \
5089
adduser -D -u 1001 -G appgroup appuser

0 commit comments

Comments
 (0)