You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# ============================================
# A股智投分析平台 - 后端服务 Dockerfile
# 多阶段构建,生产环境优化
# ============================================
# 阶段1构建阶段
FROM node:20-alpine AS builder
WORKDIR /app
# 安装必要依赖
RUN apk add --no-cache openssl
# 复制 package.json 和 package-lock.json
COPY package*.json ./
COPY prisma ./prisma/
# 安装所有依赖(包括开发依赖)
RUN npm ci
# 生成 Prisma Client
RUN npx prisma generate
# 复制源代码
COPY . .
# 编译 TypeScript
RUN npm run build
# ============================================
# 阶段2生产阶段
FROM node:20-alpine
WORKDIR /app
# 安装必要的系统依赖
RUN apk add --no-cache openssl wget
# 创建日志目录
RUN mkdir -p logs
# 复制 package.json 和 package-lock.json
COPY package*.json ./
COPY prisma ./prisma/
# 安装生产依赖(不包含开发依赖)
RUN npm ci --only=production && npm cache clean --force
# 重新生成 Prisma Client针对生产环境
RUN npx prisma generate
# 从构建阶段复制编译后的代码
COPY --from=builder /app/dist ./dist
# 创建非 root 用户
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nodejs -u 1001
RUN chown -R nodejs:nodejs /app/logs
USER nodejs
# 暴露端口
EXPOSE 3000
# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:3000/api/v1/health || exit 1
# 启动命令
CMD ["node", "dist/app.js"]