# 多阶段构建 # 第一阶段:构建应用 FROM node:18-alpine as builder # 设置工作目录 WORKDIR /app # 复制package文件 COPY package*.json ./ # 安装依赖 RUN npm ci --only=production # 复制源代码 COPY . . # 构建应用 RUN npm run build:prod # 第二阶段:运行时环境 FROM nginx:alpine # 安装必要的工具 RUN apk add --no-cache curl # 复制构建产物 COPY --from=builder /app/dist /usr/share/nginx/html # 复制Nginx配置 COPY nginx.conf /etc/nginx/conf.d/default.conf # 创建非root用户 RUN addgroup -g 1001 -S nginx && \ adduser -S nginx -u 1001 # 设置权限 RUN chown -R nginx:nginx /usr/share/nginx/html && \ chown -R nginx:nginx /var/cache/nginx && \ chown -R nginx:nginx /var/log/nginx && \ chown -R nginx:nginx /etc/nginx/conf.d # 切换到非root用户 USER nginx # 暴露端口 EXPOSE 80 443 # 健康检查 HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ CMD curl -f http://localhost/ || exit 1 # 启动Nginx CMD ["nginx", "-g", "daemon off;"]