build(Dockerfile): 优化 Docker 镜像构建过程
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
- 移除多阶段构建,简化镜像结构 - 添加 Alpine Linux 国内镜像加速配置 - 安装 tzdata 并设置中国时区 - 更新 npm 配置和安装过程 - 调整 CMD 命令为使用 npm 脚本启动应用
This commit is contained in:
parent
6b768babfd
commit
451f0a44e6
49
Dockerfile
49
Dockerfile
@ -1,37 +1,32 @@
|
||||
# 构建阶段
|
||||
FROM node:22-alpine AS builder
|
||||
#
|
||||
WORKDIR /app
|
||||
|
||||
COPY package.json .
|
||||
COPY package-lock.json* .
|
||||
|
||||
# 设置npm镜像
|
||||
RUN npm config set registry https://registry.npmmirror.com
|
||||
|
||||
RUN npm install -g pnpm
|
||||
RUN pnpm config set registry https://registry.npmmirror.com
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN npm run build
|
||||
|
||||
# 运行阶段
|
||||
FROM node:22-alpine
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY --from=builder /app/dist ./dist
|
||||
COPY --from=builder /app/package.json .
|
||||
COPY --from=builder /app/bootstrap.js .
|
||||
# 配置alpine国内镜像加速
|
||||
RUN sed -i "s@http://dl-cdn.alpinelinux.org/@https://repo.huaweicloud.com/@g" /etc/apk/repositories
|
||||
|
||||
# 设置npm镜像
|
||||
RUN pnpm config set registry https://registry.npmmirror.com
|
||||
RUN pnpm install --production
|
||||
# 安装tzdata,默认的alpine基础镜像不包含时区组件,安装后可通过TZ环境变量配置时区
|
||||
RUN apk add --no-cache tzdata
|
||||
|
||||
ENV NODE_ENV=production
|
||||
ENV PORT=8001
|
||||
# 设置时区为中国东八区,这里的配置可以被docker-compose.yml或docker run时指定的时区覆盖
|
||||
ENV TZ="Asia/Shanghai"
|
||||
|
||||
# 如果各公司有自己的私有源,可以替换registry地址,如使用官方源注释下一行
|
||||
RUN npm config set registry https://registry.npmmirror.com
|
||||
|
||||
# 安装开发期依赖
|
||||
COPY package.json ./package.json
|
||||
RUN npm install
|
||||
# 构建项目
|
||||
COPY . .
|
||||
RUN npm run build
|
||||
# 删除开发期依赖
|
||||
RUN rm -rf node_modules && rm package-lock.json
|
||||
# 安装生产环境依赖
|
||||
RUN npm install --production
|
||||
|
||||
# 如果端口更换,这边可以更新一下
|
||||
EXPOSE 8001
|
||||
|
||||
CMD ["node", "bootstrap.js"]
|
||||
CMD ["npm", "run", "start"]
|
Loading…
Reference in New Issue
Block a user