fix(blockchain-service): standardize Dockerfile with other services

- Use node:20-slim instead of alpine for OpenSSL compatibility
- Add startup script with prisma migrate/push
- Increase healthcheck start-period to 60s
- Add non-root user for security

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2025-12-07 01:17:55 -08:00
parent 1125dd98ef
commit be6abd3034
1 changed files with 49 additions and 20 deletions

View File

@ -1,54 +1,83 @@
# Build stage # =============================================================================
# Blockchain Service Dockerfile
# =============================================================================
# Build stage - use Alpine for smaller build context
FROM node:20-alpine AS builder FROM node:20-alpine AS builder
WORKDIR /app WORKDIR /app
# Copy package files # Copy package files
COPY package*.json ./ COPY package*.json ./
COPY tsconfig*.json ./
COPY nest-cli.json ./
# Copy Prisma schema
COPY prisma ./prisma/ COPY prisma ./prisma/
# Install dependencies # Install dependencies
RUN npm ci RUN npm ci
# Generate Prisma client # Generate Prisma client (dummy DATABASE_URL for build time only)
RUN npx prisma generate RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate
# Copy source code # Copy source code
COPY . . COPY src ./src
# Build # Build TypeScript
RUN npm run build RUN npm run build
# Production stage # Verify build output exists
FROM node:20-alpine AS production RUN ls -la dist/ && test -f dist/main.js
# Production stage - use Debian slim for OpenSSL compatibility
FROM node:20-slim
WORKDIR /app WORKDIR /app
# Install OpenSSL for Prisma and curl for healthcheck # Install OpenSSL and curl for health checks
RUN apk add --no-cache openssl curl RUN apt-get update && apt-get install -y --no-install-recommends \
openssl \
# Copy package files curl \
COPY package*.json ./ && rm -rf /var/lib/apt/lists/*
COPY prisma ./prisma/
# Install production dependencies only # Install production dependencies only
COPY package*.json ./
RUN npm ci --only=production RUN npm ci --only=production
# Generate Prisma client # Copy Prisma schema and generate client
RUN npx prisma generate COPY prisma ./prisma/
RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate
# Copy built application # Copy built files
COPY --from=builder /app/dist ./dist COPY --from=builder /app/dist ./dist
# Set environment # Create startup script that runs migrations before starting the app
RUN echo '#!/bin/sh\n\
set -e\n\
echo "Running database migrations..."\n\
npx prisma migrate deploy || npx prisma db push --accept-data-loss\n\
echo "Starting application..."\n\
exec node dist/main.js\n' > /app/start.sh && chmod +x /app/start.sh
# Create non-root user
RUN groupadd -g 1001 nodejs && \
useradd -u 1001 -g nodejs nestjs
# Change ownership of app directory
RUN chown -R nestjs:nodejs /app
# Switch to non-root user
USER nestjs
ENV NODE_ENV=production ENV NODE_ENV=production
# Expose port # Expose port
EXPOSE 3012 EXPOSE 3012
# Health check # Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
CMD curl -f http://localhost:3012/api/v1/health || exit 1 CMD curl -f http://localhost:3012/api/v1/health || exit 1
# Start application with database migration # Start service with migration
CMD ["sh", "-c", "npx prisma db push --skip-generate && node dist/main.js"] CMD ["/app/start.sh"]