Files
tt-booking/Dockerfile.production
T

81 lines
2.5 KiB
Docker

# Multi-stage production Dockerfile for LCC Table Tennis Booking
FROM node:22-slim AS base
# Install dependencies only when needed
FROM base AS deps
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
WORKDIR /app
# Install dependencies based on the preferred package manager
COPY package.json package-lock.json* ./
RUN \
if [ -f package-lock.json ]; then npm ci --ignore-scripts; \
else echo "Lockfile not found." && exit 1; \
fi
# Rebuild the source code only when needed
FROM node:22-alpine AS builder
RUN apk add --no-cache python3 make g++ curl
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Rebuild better-sqlite3 for Alpine Linux
RUN npm rebuild better-sqlite3
# Build TypeScript database scripts to JavaScript
RUN node build-scripts.js
# Build the application
RUN npm run build
# Remove development-only dependencies and dedupe after build
RUN npm prune --omit=dev && npm dedupe --prod \
&& find node_modules -type f \( -name "README" -o -name "README.*" -o -name "CHANGELOG*" -o -name "*.md" -o -name "*.map" \) -delete \
&& find node_modules -type d \( -name "__tests__" -o -name "test" -o -name "tests" -o -name "docs" -o -name "examples" \) -prune -exec rm -rf {} + \
&& npm cache clean --force
# Production image, copy all the files and run next
FROM node:22-alpine AS runner
RUN apk add --no-cache curl
WORKDIR /app
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
# Copy necessary files from builder stage
COPY --from=builder /app/.next/standalone ./
COPY --from=builder --chown=root:root /app/.next/static ./.next/static
# Copy compiled database scripts instead of TypeScript sources
COPY --from=builder /app/dist ./dist
# Copy minimal runtime dependencies for database operations
COPY --from=builder /app/node_modules ./node_modules
# Copy database config and package.json
COPY --from=builder /app/lib ./lib
COPY --from=builder /app/drizzle.config.ts ./drizzle.config.ts
COPY --from=builder /app/package.json ./package.json
# Copy entrypoint script
COPY docker-entrypoint-alpine.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
# Create public directory if it doesn't exist
RUN mkdir -p public
# Create directories for data and backups
RUN mkdir -p /app/data /app/backups /app/logs
EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
CMD curl -f http://localhost:3000/api/health || exit 1
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["node", "server.js"]