-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
98 lines (72 loc) Β· 3.17 KB
/
Dockerfile
File metadata and controls
98 lines (72 loc) Β· 3.17 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
# ---- Base Stage ----
FROM node:24-alpine AS base
WORKDIR /app
# Install dependencies only when needed
RUN apk add --no-cache libc6-compat python3 make g++
# ---- Dependencies Stage ----
FROM base AS deps
# Copy package files and Prisma config
COPY package*.json ./
COPY prisma.config.ts ./
COPY db/schema.prisma ./db/
# Install dependencies for production (save to /tmp for later)
RUN npm ci --only=production --ignore-scripts && \
cp -R node_modules /tmp/prod_node_modules
# Install all dependencies (including dev) for build stage
RUN npm ci && \
npx prisma generate
# ---- Builder Stage ----
FROM base AS builder
# Copy source files first
COPY . .
# Copy dependencies and generated Prisma client from deps stage
COPY --from=deps /app/node_modules ./node_modules
COPY --from=deps /app/db/generated ./db/generated
# Set environment variables for build
ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=production
ENV DATABASE_URL=file:./db/app.db
# Create database tables before GTFS import
RUN npx prisma db push
# Generate track.json before build (required by build process)
RUN npx tsx -e "import('./app/lib/gtfs-import.ts').then(m => m.importGtfsData())"
# Build the Next.js application with standalone output
# Increase Node.js heap size to handle builds on low-memory VPS (1GB RAM + 2GB swap)
ENV NODE_OPTIONS="--max-old-space-size=1536"
RUN npm run build
# ---- Runner Stage ----
FROM node:24-alpine AS runner
WORKDIR /app
# Install runtime dependencies
# - libc6-compat: required for Node.js native modules
# - su-exec: lightweight alternative to gosu for privilege dropping
RUN apk add --no-cache libc6-compat su-exec
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
# Create non-root user
RUN addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 nextjs
# Copy Next.js build artifacts
COPY --chown=nextjs:nodejs --from=builder /app/public ./public
COPY --chown=nextjs:nodejs --from=builder /app/.next/standalone ./
COPY --chown=nextjs:nodejs --from=builder /app/.next/static ./.next/static
# Copy Prisma and DB files (schema + migrations, not the database itself)
COPY --chown=nextjs:nodejs --from=builder /app/prisma.config.ts ./prisma.config.ts
COPY --chown=nextjs:nodejs --from=builder /app/db ./db
# Create data directory for persistent database (mounted as volume in docker-compose)
RUN mkdir -p /app/data && chown nextjs:nodejs /app/data
# Copy runtime data
COPY --chown=nextjs:nodejs --from=builder /app/app/api/stations/amtrak-stations.csv ./app/api/stations/amtrak-stations.csv
# Copy production node_modules
COPY --chown=nextjs:nodejs --from=deps /tmp/prod_node_modules ./node_modules
# Copy Prisma CLI from builder (needed for migrations at runtime)
COPY --chown=nextjs:nodejs --from=builder /app/node_modules/.bin/prisma ./node_modules/.bin/prisma
COPY --chown=nextjs:nodejs --from=builder /app/node_modules/prisma ./node_modules/prisma
# Copy entrypoint script (needs to run as root initially for permission fixes)
COPY --chmod=755 docker/docker-entrypoint.sh /usr/local/bin/
EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
# Run as root initially to allow permission fixes, then drop to nextjs in entrypoint
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["node", "server.js"]