87 lines
2.3 KiB
Docker
87 lines
2.3 KiB
Docker
# =============================================================================
|
|
# Leaderboard Service Dockerfile
|
|
# =============================================================================
|
|
|
|
# Build stage - use Debian slim for better Prisma compatibility
|
|
FROM node:20-slim AS builder
|
|
|
|
WORKDIR /app
|
|
|
|
# Install OpenSSL for Prisma
|
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
openssl \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
# Copy package files
|
|
COPY package*.json ./
|
|
COPY tsconfig*.json ./
|
|
COPY nest-cli.json ./
|
|
COPY prisma ./prisma/
|
|
|
|
# Install dependencies
|
|
RUN npm ci
|
|
|
|
# Generate Prisma client (dummy DATABASE_URL for build time only)
|
|
RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate
|
|
|
|
# Copy source code
|
|
COPY src ./src
|
|
|
|
# Build the application
|
|
RUN npm run build
|
|
|
|
# Verify build output exists
|
|
RUN ls -la dist/src/ && test -f dist/src/main.js
|
|
|
|
# Production stage - use Debian slim for OpenSSL compatibility
|
|
FROM node:20-slim AS production
|
|
|
|
WORKDIR /app
|
|
|
|
# Install OpenSSL and curl for health checks
|
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
openssl \
|
|
curl \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
# Copy package files and install production dependencies
|
|
COPY package*.json ./
|
|
RUN npm ci --only=production
|
|
|
|
# Copy Prisma files and generate client (dummy DATABASE_URL for build time only)
|
|
COPY prisma ./prisma/
|
|
RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate
|
|
|
|
# Copy built application
|
|
COPY --from=builder /app/dist ./dist
|
|
|
|
# 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/src/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
|
|
|
|
# Expose port
|
|
EXPOSE 3007
|
|
|
|
# Health check
|
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
|
|
CMD curl -f http://localhost:3007/api/health || exit 1
|
|
|
|
# Start service with migration
|
|
CMD ["/app/start.sh"]
|