fix(reward-service): 优化 Dockerfile 和添加 .dockerignore
问题: 1. Dockerfile 使用 COPY . . 会复制 node_modules 导致构建极慢 2. CMD 路径错误:dist/src/main.js 应该是 dist/main.js 修复: - 添加 .dockerignore 排除 node_modules, dist 等 - 优化 Dockerfile 只复制必要文件 (COPY src ./src) - 修正 CMD 路径为 dist/main.js - 添加构建验证步骤 (test -f dist/main.js) - 添加非 root 用户运行 - 添加健康检查 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
fc26575693
commit
c3931255d3
|
|
@ -0,0 +1,50 @@
|
|||
# Dependencies (will be installed fresh in container)
|
||||
node_modules/
|
||||
|
||||
# Build output (will be built in container)
|
||||
dist/
|
||||
|
||||
# Environment files (will be provided at runtime)
|
||||
.env
|
||||
.env.local
|
||||
.env.development
|
||||
.env.development.local
|
||||
.env.test
|
||||
.env.test.local
|
||||
.env.production
|
||||
.env.production.local
|
||||
|
||||
# Git
|
||||
.git/
|
||||
.gitignore
|
||||
|
||||
# IDE
|
||||
.idea/
|
||||
.vscode/
|
||||
*.swp
|
||||
*.swo
|
||||
|
||||
# OS
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# Test
|
||||
coverage/
|
||||
.nyc_output
|
||||
|
||||
# Logs
|
||||
logs/
|
||||
*.log
|
||||
|
||||
# Docker
|
||||
Dockerfile
|
||||
docker-compose.yml
|
||||
.dockerignore
|
||||
|
||||
# Documentation
|
||||
*.md
|
||||
*.png
|
||||
docs/
|
||||
|
||||
# Claude
|
||||
.claude/
|
||||
|
|
@ -1,3 +1,7 @@
|
|||
# =============================================================================
|
||||
# Reward Service Dockerfile
|
||||
# =============================================================================
|
||||
|
||||
# Build stage
|
||||
FROM node:20-alpine AS builder
|
||||
|
||||
|
|
@ -5,22 +9,27 @@ WORKDIR /app
|
|||
|
||||
# Copy package files
|
||||
COPY package*.json ./
|
||||
COPY tsconfig*.json ./
|
||||
COPY nest-cli.json ./
|
||||
COPY prisma ./prisma/
|
||||
|
||||
# Install dependencies
|
||||
RUN npm ci
|
||||
|
||||
# Copy source code
|
||||
COPY . .
|
||||
|
||||
# Generate Prisma client (dummy DATABASE_URL for build time only)
|
||||
RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate
|
||||
|
||||
# Build the application
|
||||
# Copy source code
|
||||
COPY src ./src
|
||||
|
||||
# Build TypeScript
|
||||
RUN npm run build
|
||||
|
||||
# Verify build output exists
|
||||
RUN ls -la dist/ && test -f dist/main.js
|
||||
|
||||
# Production stage - use Debian slim for OpenSSL compatibility
|
||||
FROM node:20-slim AS production
|
||||
FROM node:20-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
|
|
@ -30,24 +39,32 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
|||
wget \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Copy package files and install production dependencies
|
||||
# Install production dependencies only
|
||||
COPY package*.json ./
|
||||
RUN npm ci --only=production
|
||||
|
||||
# Copy Prisma files
|
||||
# Copy Prisma schema and generate client
|
||||
COPY prisma ./prisma/
|
||||
|
||||
# Generate Prisma client for production (dummy DATABASE_URL for build time only)
|
||||
RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate
|
||||
|
||||
# Copy built application from builder stage
|
||||
# Copy built files
|
||||
COPY --from=builder /app/dist ./dist
|
||||
|
||||
# Set environment
|
||||
ENV NODE_ENV=production
|
||||
ENV PORT=3005
|
||||
# Create non-root user
|
||||
RUN groupadd -g 1001 nodejs && \
|
||||
useradd -u 1001 -g nodejs nestjs
|
||||
|
||||
# Switch to non-root user
|
||||
USER nestjs
|
||||
|
||||
ENV NODE_ENV=production
|
||||
|
||||
# Expose port
|
||||
EXPOSE 3005
|
||||
|
||||
# Run the application
|
||||
CMD ["node", "dist/src/main.js"]
|
||||
# Health check
|
||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
|
||||
CMD wget -q --spider http://localhost:3005/health || exit 1
|
||||
|
||||
# Start service
|
||||
CMD ["node", "dist/main.js"]
|
||||
|
|
|
|||
Loading…
Reference in New Issue