|
1 | | -# Stage 1: Dependencies |
2 | | -FROM public.ecr.aws/lambda/nodejs:18-x86_64 AS deps |
| 1 | +# syntax=docker/dockerfile:1 |
| 2 | + |
| 3 | +# ========================================== |
| 4 | +# BASE STAGE - Common setup for all stages |
| 5 | +# ========================================== |
| 6 | +FROM oven/bun:1 AS base |
3 | 7 | WORKDIR /app |
4 | 8 |
|
5 | | -# Install unzip (required for Bun installer) and Bun |
6 | | -RUN yum update -y && yum install -y unzip && \ |
7 | | - curl -fsSL https://bun.sh/install | bash && \ |
8 | | - ln -s /root/.bun/bin/bun /usr/local/bin/bun |
| 9 | +# ========================================== |
| 10 | +# DEPS STAGE - Install production dependencies |
| 11 | +# ========================================== |
| 12 | +FROM base AS deps |
| 13 | +# Copy package files for dependency installation |
| 14 | +COPY package.json bun.lockb* ./ |
| 15 | +# Install production dependencies only |
| 16 | +RUN bun install --frozen-lockfile --production |
| 17 | + |
| 18 | +# ========================================== |
| 19 | +# BUILD STAGE - Build the Next.js application |
| 20 | +# ========================================== |
| 21 | +FROM base AS builder |
| 22 | +WORKDIR /app |
9 | 23 |
|
10 | | -# Copy only package files first (for better caching) |
11 | | -COPY package.json bun.lock* ./ |
| 24 | +# Copy package files |
| 25 | +COPY package.json bun.lockb* ./ |
| 26 | +# Install all dependencies (including devDependencies needed for build) |
12 | 27 | RUN bun install --frozen-lockfile |
13 | 28 |
|
14 | | -# Stage 2: Builder |
15 | | -FROM public.ecr.aws/lambda/nodejs:18-x86_64 AS builder |
16 | | -WORKDIR /app |
| 29 | +# Copy source code |
| 30 | +COPY . . |
17 | 31 |
|
18 | | -# Install unzip (required for Bun installer) and Bun |
19 | | -RUN yum update -y && yum install -y unzip && \ |
20 | | - curl -fsSL https://bun.sh/install | bash && \ |
21 | | - ln -s /root/.bun/bin/bun /usr/local/bin/bun |
| 32 | +# Copy Prisma schema and generate client |
| 33 | +COPY prisma ./prisma |
| 34 | +RUN bunx prisma generate |
22 | 35 |
|
23 | | -# Accept DATABASE_URL as build argument |
| 36 | +# Build arguments for compile-time environment variables |
24 | 37 | ARG DATABASE_URL |
25 | | -ENV DATABASE_URL=$DATABASE_URL |
26 | | -ENV NODE_ENV=production |
| 38 | +ARG NODE_ENV=production |
27 | 39 |
|
28 | | -# Copy dependencies from previous stage |
29 | | -COPY --from=deps /app/node_modules ./node_modules |
30 | | -COPY . . |
| 40 | +# Set environment variables for build |
| 41 | +ENV DATABASE_URL=$DATABASE_URL |
| 42 | +ENV NODE_ENV=$NODE_ENV |
| 43 | +ENV NEXT_TELEMETRY_DISABLED=1 |
31 | 44 |
|
32 | 45 | # Build the application |
33 | 46 | RUN bun run build |
34 | 47 |
|
35 | | -# Stage 3: Runner |
36 | | -FROM public.ecr.aws/lambda/nodejs:18-x86_64 AS runner |
| 48 | +# ========================================== |
| 49 | +# RUNNER STAGE - Production runtime |
| 50 | +# ========================================== |
| 51 | +FROM base AS runner |
37 | 52 | WORKDIR /app |
38 | 53 |
|
39 | | -# Install unzip (required for Bun installer) and Bun |
40 | | -RUN yum update -y && yum install -y unzip && \ |
41 | | - curl -fsSL https://bun.sh/install | bash && \ |
42 | | - ln -s /root/.bun/bin/bun /usr/local/bin/bun |
43 | | - |
| 54 | +# Set production environment |
44 | 55 | ENV NODE_ENV=production |
45 | 56 | ENV NEXT_TELEMETRY_DISABLED=1 |
| 57 | +ENV PORT=3000 |
| 58 | +ENV HOSTNAME="0.0.0.0" |
46 | 59 |
|
47 | | -# Cache bust to force rebuild |
48 | | -LABEL build_date="2024-12-08-15:08" |
| 60 | +# Create non-root user for security |
| 61 | +RUN addgroup --system --gid 1001 nodejs && \ |
| 62 | + adduser --system --uid 1001 nextjs |
49 | 63 |
|
50 | | -# Copy only necessary files |
| 64 | +# Copy necessary files from builder stage |
51 | 65 | COPY --from=builder /app/public ./public |
52 | | -COPY --from=builder /app/.next/standalone ./ |
53 | | -COPY --from=builder /app/.next/static ./.next/static |
| 66 | +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ |
| 67 | +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static |
54 | 68 |
|
55 | | -# Copy migration files and scripts for database migrations |
56 | | -COPY --from=builder /app/src ./src |
57 | | -COPY --from=builder /app/scripts ./scripts |
58 | | -COPY --from=builder /app/package.json ./package.json |
59 | | -COPY --from=builder /app/drizzle.config.ts ./drizzle.config.ts |
60 | | -COPY --from=builder /app/node_modules ./node_modules |
| 69 | +# Switch to non-root user |
| 70 | +USER nextjs |
61 | 71 |
|
| 72 | +# Expose the application port |
62 | 73 | EXPOSE 3000 |
63 | 74 |
|
64 | | -ENV PORT=3000 |
65 | | -ENV HOSTNAME="0.0.0.0" |
66 | | - |
| 75 | +# Start the application |
67 | 76 | CMD ["bun", "server.js"] |
68 | 77 |
|
0 commit comments