56 lines
1.4 KiB
Docker
56 lines
1.4 KiB
Docker
# 国内网络优化版 Dockerfile
|
|
# 使用国内镜像源和 npm 镜像加速构建
|
|
|
|
# 第一阶段:构建 React 应用
|
|
FROM node:20-alpine AS builder
|
|
|
|
WORKDIR /app
|
|
|
|
# 配置 npm 使用国内淘宝镜像源
|
|
RUN npm config set registry https://registry.npmmirror.com
|
|
|
|
# 复制 package 文件
|
|
COPY package*.json ./
|
|
|
|
# 安装依赖(使用 ci 命令确保一致性)
|
|
RUN npm ci --prefer-offline --no-audit
|
|
|
|
# 复制源代码
|
|
COPY . .
|
|
|
|
# 构建生产版本
|
|
RUN npm run build
|
|
|
|
# 第二阶段:使用 Nginx 托管静态文件
|
|
FROM nginx:1.27-alpine
|
|
|
|
# 使用阿里云镜像源替换默认的 Alpine 源
|
|
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && \
|
|
apk update && \
|
|
apk add --no-cache tzdata && \
|
|
rm -rf /var/cache/apk/*
|
|
|
|
# 设置时区为上海
|
|
ENV TZ=Asia/Shanghai
|
|
RUN cp /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
|
|
|
|
WORKDIR /usr/share/nginx/html
|
|
|
|
# 从构建阶段复制构建好的文件
|
|
COPY --from=builder /app/dist .
|
|
|
|
# 复制 Nginx 配置
|
|
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
|
|
|
# 创建健康检查文件
|
|
RUN echo "OK" > /usr/share/nginx/html/health
|
|
|
|
# 暴露端口
|
|
EXPOSE 80
|
|
|
|
# 健康检查
|
|
HEALTHCHECK --interval=30s --timeout=10s --start-period=20s --retries=3 \
|
|
CMD wget --no-verbose --tries=1 --spider http://localhost:80/health || exit 1
|
|
|
|
# 启动 Nginx
|
|
CMD ["nginx", "-g", "daemon off;"] |