improvement: docker infrastructure overhauled.
This commit is contained in:
@@ -1,92 +1,58 @@
|
||||
# ============================================
|
||||
# STAGE 1: BUILDER STAGE
|
||||
# ============================================
|
||||
# This stage builds the application and prepares assets
|
||||
FROM node:18-alpine AS builder
|
||||
|
||||
# Set working directory
|
||||
WORKDIR /app
|
||||
|
||||
# Copy package.json and package-lock.json first (for better caching)
|
||||
COPY package*.json ./
|
||||
|
||||
# Copy Prisma schema (needed for Prisma client generation)
|
||||
COPY prisma ./prisma/
|
||||
|
||||
# Install ALL dependencies (including dev dependencies for build)
|
||||
RUN npm ci
|
||||
|
||||
# Copy source code
|
||||
COPY . .
|
||||
|
||||
# Generate Prisma client
|
||||
RUN npx prisma generate
|
||||
|
||||
# Build the NestJS application
|
||||
RUN npm run build
|
||||
|
||||
# Remove development dependencies to reduce size
|
||||
RUN npm prune --production
|
||||
|
||||
|
||||
# ============================================
|
||||
# STAGE 2: PRODUCTION STAGE
|
||||
# ============================================
|
||||
# This stage creates the final production image
|
||||
FROM node:18-alpine
|
||||
|
||||
# Install dumb-init for proper signal handling
|
||||
RUN apk add --no-cache dumb-init
|
||||
|
||||
# Set working directory
|
||||
WORKDIR /app
|
||||
|
||||
# Create non-root user for security
|
||||
RUN addgroup -g 1001 -S nodejs && \
|
||||
adduser -S dyolink -u 1001
|
||||
|
||||
# Copy package.json files
|
||||
COPY package*.json ./
|
||||
|
||||
# Copy Prisma schema
|
||||
COPY prisma ./prisma/
|
||||
|
||||
# Install ONLY production dependencies
|
||||
RUN npm ci --only=production && \
|
||||
npm cache clean --force
|
||||
|
||||
# Generate Prisma client in production
|
||||
RUN npx prisma generate
|
||||
|
||||
# Copy built application from builder stage
|
||||
COPY --from=builder /app/dist ./dist
|
||||
|
||||
# Copy node_modules (already pruned)
|
||||
COPY --from=builder /app/node_modules ./node_modules
|
||||
|
||||
# Create necessary directories with proper permissions
|
||||
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
|
||||
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
RUN mkdir -p /app/logs && \
|
||||
chown -R dyolink:nodejs /app
|
||||
|
||||
# Set ownership of all files to non-root user
|
||||
RUN chown -R dyolink:nodejs /app
|
||||
|
||||
# Switch to non-root user
|
||||
USER dyolink
|
||||
|
||||
# Expose the application port
|
||||
EXPOSE 3000
|
||||
|
||||
# Health check configuration
|
||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=40s --retries=3 \
|
||||
CMD node -e "require('http').get('http://localhost:3000/api/health', (r) => {if(r.statusCode!==200)throw new Error()})" || exit 1
|
||||
CMD node -e "require('http').get('http://127.0.0.1:3000/api/health', (r) => {if(r.statusCode!==200)process.exit(1)})"
|
||||
|
||||
# Copy entrypoint script
|
||||
COPY docker-entrypoint.sh /usr/local/bin/
|
||||
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# Use dumb-init to properly handle signals
|
||||
ENTRYPOINT ["dumb-init", "--", "docker-entrypoint.sh"]
|
||||
|
||||
# Start the application
|
||||
CMD ["node", "dist/main"]
|
||||
CMD ["node", "dist/main"]
|
||||
|
||||
@@ -1,76 +1,36 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
|
||||
# ============================================
|
||||
# DOCKER ENTRYPOINT SCRIPT
|
||||
# This script runs BEFORE the application starts
|
||||
# ============================================
|
||||
echo "=========================================="
|
||||
echo " Dyolink Backend - Docker Entrypoint"
|
||||
echo "=========================================="
|
||||
|
||||
# Colors for logging (optional, for better readability)
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
echo "${GREEN}========================================${NC}"
|
||||
echo "${GREEN} Dyolink Backend - Docker Entrypoint ${NC}"
|
||||
echo "${GREEN}========================================${NC}"
|
||||
|
||||
# Check if we're in development or production
|
||||
if [ "$NODE_ENV" = "production" ]; then
|
||||
echo "${GREEN}Running in PRODUCTION mode${NC}"
|
||||
|
||||
# Run database migrations
|
||||
echo "${YELLOW}Running database migrations...${NC}"
|
||||
echo "Running in PRODUCTION mode"
|
||||
echo "Running database migrations..."
|
||||
npx prisma migrate deploy
|
||||
|
||||
# Check if migrations were successful
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "${GREEN}✓ Database migrations completed successfully${NC}"
|
||||
else
|
||||
echo "${RED}✗ Database migrations failed!${NC}"
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
echo "${YELLOW}Running in DEVELOPMENT mode${NC}"
|
||||
|
||||
# In development, we might want to push schema instead of migrations
|
||||
echo "${YELLOW}Syncing database schema...${NC}"
|
||||
echo "Running in DEVELOPMENT mode"
|
||||
echo "Syncing database schema..."
|
||||
npx prisma db push
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "${GREEN}✓ Database schema synced successfully${NC}"
|
||||
else
|
||||
echo "${RED}✗ Database schema sync failed!${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ "$NODE_ENV" != "production" ]; then
|
||||
if [ -f "prisma/seed.ts" ] || [ -f "prisma/seed.js" ]; then
|
||||
echo "Running database seed..."
|
||||
npx prisma db seed
|
||||
fi
|
||||
fi
|
||||
|
||||
# Optional: Run seed script if it exists and NODE_ENV is not production
|
||||
if [ "$NODE_ENV" != "production" ] && [ -f "prisma/seed.js" ]; then
|
||||
echo "${YELLOW}Running database seed...${NC}"
|
||||
npx prisma db seed
|
||||
echo "${GREEN}✓ Database seeded successfully${NC}"
|
||||
fi
|
||||
|
||||
# Verify database connection
|
||||
echo "${YELLOW}Verifying database connection...${NC}"
|
||||
npx prisma db execute --file /dev/null --schema prisma/schema.prisma 2>/dev/null
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "${GREEN}✓ Database connection verified${NC}"
|
||||
else
|
||||
echo "${RED}✗ Cannot connect to database!${NC}"
|
||||
echo "Verifying database connection..."
|
||||
if ! echo "SELECT 1" | npx prisma db execute --stdin --schema prisma/schema.prisma >/dev/null 2>&1; then
|
||||
echo "Cannot connect to database or execute query."
|
||||
exit 1
|
||||
fi
|
||||
echo "Database connection OK"
|
||||
|
||||
# Print application information
|
||||
echo "${GREEN}========================================${NC}"
|
||||
echo "${GREEN}Starting Dyolink Backend Application...${NC}"
|
||||
echo "${GREEN} • Environment: ${NODE_ENV:-development}${NC}"
|
||||
echo "${GREEN} • Port: ${PORT:-3000}${NC}"
|
||||
echo "${GREEN} • Database: ${DATABASE_URL%%@*}@***${NC}"
|
||||
echo "${GREEN}========================================${NC}"
|
||||
echo "Starting Dyolink Backend..."
|
||||
echo " Environment: ${NODE_ENV:-development}"
|
||||
echo " Port: ${PORT:-3000}"
|
||||
|
||||
# Execute the main command (passed as CMD)
|
||||
exec "$@"
|
||||
exec "$@"
|
||||
|
||||
@@ -22,4 +22,13 @@ export class AppController {
|
||||
getHello(): string {
|
||||
return this.appService.getHello();
|
||||
}
|
||||
|
||||
/** Used by Docker / load balancer health checks (GET /api/health) */
|
||||
@Get('health')
|
||||
health() {
|
||||
return {
|
||||
status: 'ok',
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user