-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDockerfile
More file actions
128 lines (95 loc) · 4.23 KB
/
Dockerfile
File metadata and controls
128 lines (95 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Production Dockerfile for Lion Reader
# Multi-stage build for optimal image size
# =============================================================================
# Stage 1: Base image with pnpm (for building)
# =============================================================================
FROM node:24-alpine AS base
# Enable corepack and prepare the exact pnpm version from package.json
RUN corepack enable && corepack prepare pnpm@10.26.2 --activate
# Set working directory
WORKDIR /app
# =============================================================================
# Stage 2: Install dependencies
# =============================================================================
FROM base AS deps
# Copy package files
COPY package.json pnpm-lock.yaml ./
# Install all dependencies (including devDependencies for building)
# Use --ignore-scripts because postinstall needs files not yet copied
RUN --mount=type=cache,id=pnpm,target=/root/.local/share/pnpm/store \
pnpm install --frozen-lockfile --ignore-scripts
# =============================================================================
# Stage 3: Build the application
# =============================================================================
FROM base AS builder
WORKDIR /app
# Copy dependencies from deps stage
COPY --from=deps /app/node_modules ./node_modules
# Copy source code
COPY . .
# Run postinstall script (copies ONNX WASM files to public/)
RUN node scripts/copy-onnx-wasm.mjs
# Set environment for build
ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=production
# Dummy URLs for build - modules check these exist but don't connect
ENV DATABASE_URL="postgresql://build:build@localhost:5432/build"
ENV REDIS_URL="redis://localhost:6379"
# Build Next.js application
RUN pnpm build
# Build custom server bundle (compression + Next.js wrapper)
RUN pnpm build:server
# Build worker bundle (single optimized JS file)
RUN pnpm build:worker
# Build worker-thread bundle (piscina entry point for CPU-intensive tasks)
RUN pnpm build:worker-thread
# Build Discord bot bundle (single optimized JS file)
RUN pnpm build:discord-bot
# Build migration bundle (single optimized JS file)
RUN pnpm build:migrate
# Prune dev dependencies after build
RUN --mount=type=cache,id=pnpm,target=/root/.local/share/pnpm/store \
pnpm prune --prod --ignore-scripts
# =============================================================================
# Stage 4: Production runner (minimal image, no pnpm needed)
# =============================================================================
FROM node:24-alpine AS runner
WORKDIR /app
# Install bash for startup script and create non-root user
RUN apk add --no-cache bash && \
addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 nextjs
# Set production environment
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
# Copy necessary files for running the app
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json
# Copy production node_modules (already pruned in builder)
COPY --from=builder /app/node_modules ./node_modules
# Copy built Next.js app
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
# Copy migrations (SQL files needed at runtime)
COPY --from=builder --chown=nextjs:nodejs /app/migrations ./migrations
# Copy bundled scripts (no longer need tsx, tsconfig, or src/)
COPY --from=builder /app/dist/server.js ./dist/server.js
COPY --from=builder /app/dist/worker.js ./dist/worker.js
COPY --from=builder /app/dist/worker-thread.js ./dist/worker-thread.js
COPY --from=builder /app/dist/migrate.js ./dist/migrate.js
COPY --from=builder /app/dist/discord-bot.js ./dist/discord-bot.js
# Copy startup script
COPY --from=builder /app/scripts/start-all.sh ./scripts/start-all.sh
RUN chmod +x scripts/start-all.sh
# Generate minimal next.config.js for runtime.
# The full next.config.ts requires TypeScript and build-time-only deps (next-pwa,
# sentry). Only compress:false is needed at runtime — everything else (headers,
# webpack, etc.) is baked into .next/ at build time.
RUN echo 'module.exports = { compress: false };' > next.config.js
# Switch to non-root user
USER nextjs
# Expose the port
EXPOSE 3000
# Start both API server and background worker
CMD ["./scripts/start-all.sh"]