Skip to content

Commit 552d364

Browse files
committed
Refactor : fix dockerfile
1 parent 38d879f commit 552d364

File tree

2 files changed

+35
-67
lines changed

2 files changed

+35
-67
lines changed

Diff for: Dockerfile

+29-60
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,75 @@
11
# 阶段1:基础镜像准备
22
FROM node:18-alpine AS base
33

4-
ARG USE_CN_MIRROR
5-
64
# 设置工作目录
75
WORKDIR /app
86

9-
# 配置国内镜像源(如果需要)
10-
RUN \
11-
if [ "${USE_CN_MIRROR:-false}" = "true" ]; then \
12-
npm config set registry https://registry.npmmirror.com/; \
13-
fi
7+
# 配置国内镜像源
8+
RUN npm config set registry https://registry.npmmirror.com/
149

15-
# 安装必要的系统依赖(例如CA证书
10+
# 安装必要的系统依赖(例如 CA 证书
1611
RUN apk add --no-cache ca-certificates
1712

18-
# 创建distroless目录,准备复制必要的运行时文件
19-
RUN mkdir -p /distroless/bin /distroless/lib /distroless/etc/ssl/certs /distroless/etc
20-
21-
# 复制Node.js可执行文件
22-
RUN cp /usr/local/bin/node /distroless/bin/
23-
24-
# 复制Node.js运行时依赖的库文件
25-
RUN ldd /usr/local/bin/node | awk '{print $3}' | grep -v '^$' | xargs -I '{}' cp '{}' /distroless/lib/
26-
27-
# 复制动态链接器
28-
RUN cp /lib/ld-musl-$(uname -m).so.1 /distroless/lib/
29-
30-
# 复制CA证书
31-
RUN cp -r /etc/ssl/certs /distroless/etc/ssl/
32-
33-
# 创建非root用户
34-
RUN addgroup -g 1001 appgroup && \
35-
adduser -D -u 1001 -G appgroup appuser
36-
37-
# 复制用户和组信息
38-
RUN cp /etc/passwd /distroless/etc/passwd && \
39-
cp /etc/group /distroless/etc/group
40-
41-
42-
4313
# 阶段2:构建应用程序
4414
FROM base AS builder
4515

46-
ARG USE_CN_MIRROR
47-
4816
WORKDIR /app
4917

5018
# 复制依赖文件
5119
COPY package.json yarn.lock ./
5220

53-
# 确保在构建阶段NODE_ENV不为production
54-
ENV NODE_ENV=development
55-
56-
# 配置国内镜像源并安装依赖
57-
RUN \
58-
if [ "${USE_CN_MIRROR:-false}" = "true" ]; then \
59-
npm config set registry https://registry.npmmirror.com/; \
60-
fi && \
61-
yarn install
21+
# 安装所有依赖,包括开发依赖
22+
RUN yarn install
6223

6324
# 复制项目源代码
6425
COPY . .
6526

6627
# 构建应用程序
6728
RUN yarn build
6829

69-
# 重新设置NODE_ENV为production
30+
# 删除 node_modules 目录
31+
RUN rm -rf node_modules
32+
33+
# 设置 NODE_ENV 为 production
7034
ENV NODE_ENV=production
7135

72-
# 删除devDependencies,减小最终镜像大小
36+
# 安装生产依赖
7337
RUN yarn install --production --ignore-scripts --prefer-offline
7438

75-
# 修改文件权限,使appuser拥有所有权
76-
RUN chown -R appuser:appgroup /app
77-
78-
39+
# 清理 yarn 缓存
40+
RUN yarn cache clean --all
7941

8042
# 阶段3:构建最终的生产镜像
81-
FROM scratch
43+
FROM node:18-alpine
44+
45+
# 设置工作目录
46+
WORKDIR /app
8247

83-
# 复制distroless文件
84-
COPY --from=base /distroless /
48+
# 创建非 root 用户
49+
RUN addgroup -g 1001 appgroup && \
50+
adduser -D -u 1001 -G appgroup appuser
8551

8652
# 复制应用程序文件
87-
COPY --from=builder /app /app
53+
COPY --from=builder /app/server.js /app/server.js
54+
COPY --from=builder /app/dist /app/dist
55+
COPY --from=builder /app/api /app/api
56+
COPY --from=builder /app/node_modules /app/node_modules
57+
COPY --from=builder /app/package.json /app/package.json
58+
59+
# 修改文件权限,使 appuser 拥有所有权
60+
RUN chown -R appuser:appgroup /app
8861

8962
# 设置环境变量
9063
ENV NODE_ENV=production
9164
ENV HOSTNAME="0.0.0.0"
9265
ENV PORT=13000
9366
ENV NODE_OPTIONS="--dns-result-order=ipv4first --use-openssl-ca"
9467

95-
# 设置工作目录
96-
WORKDIR /app
97-
9868
# 暴露端口
9969
EXPOSE 13000
10070

101-
# 使用非root用户
71+
# 使用非 root 用户
10272
USER appuser
10373

10474
# 启动命令
105-
ENTRYPOINT ["/bin/node"]
106-
CMD ["server.js"]
75+
CMD ["node", "server.js"]

Diff for: package.json

+6-7
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,17 @@
1111
"docker:install": "yarn install --production --frozen-lockfile && yarn build"
1212
},
1313
"dependencies": {
14+
"dotenv": "^16.4.5",
15+
"express": "^4.21.1"
16+
},
17+
"devDependencies": {
1418
"@ant-design/icons-vue": "^7.0.1",
1519
"@vueuse/core": "^11.2.0",
1620
"ant-design-vue": "^4.2.6",
17-
"axios": "^1.7.7",
18-
"dotenv": "^16.4.5",
19-
"echarts": "^5.5.1",
20-
"express": "^4.21.1",
2121
"vue": "^3.5.12",
2222
"vue-i18n": "^9.14.1",
23-
"vue-router": "^4.4.5"
24-
},
25-
"devDependencies": {
23+
"vue-router": "^4.4.5",
24+
"echarts": "^5.5.1",
2625
"@vitejs/plugin-vue": "^5.1.4",
2726
"less": "^4.2.0",
2827
"less-loader": "^12.2.0",

0 commit comments

Comments
 (0)