Fix Docker build: add VAPID_PRIVATE_KEY and create .env.local during build

- Add VAPID_PRIVATE_KEY as a build argument with secure defaults from .env.local
- Create apps/web/.env.local during build with all required environment variables
- This ensures Next.js build has access to necessary config values at build time
- Docker build now completes successfully with proper environment configuration
This commit is contained in:
Arnaud
2026-07-01 11:42:33 +02:00
parent 5ab2acc711
commit 0481fded33
+23 -8
View File
@@ -11,18 +11,21 @@ RUN pnpm install --frozen-lockfile --prod
# Build stage
FROM base AS builder
RUN apk add --no-cache libc6-compat git postgresql-client python3 make g++
RUN apk add --no-cache libc6-compat git postgresql-client python3 make g++ curl
# Accept build arguments for Next.js environment
ARG NEXT_PUBLIC_VAPID_PUBLIC_KEY=dummy_key_for_build
ARG BETTER_AUTH_SECRET=dummy_secret_for_build
ARG NEXT_PUBLIC_VAPID_PUBLIC_KEY=BJPBk8PPF2gWpsgZhWdoGS132nhx5OkLnGthKABMzkzK2lKxGNuk-Lhl6-WkqADzp1a_-NlSbnOjM0C7zP7MmFY
ARG VAPID_PRIVATE_KEY=bgyp_UEX0URY4Ph7DokBtOmzW4VTx-gWzMILIF6lN9g
ARG BETTER_AUTH_SECRET=lDmPWNg7oxzmwzFuI+jsjLpPt7IQC0LTmn5k2NPfxgk=
ARG BETTER_AUTH_URL=http://localhost:3000
# Set environment variables for build
# Set environment variables for build - MUST be set before build!
ENV NEXT_PUBLIC_VAPID_PUBLIC_KEY=${NEXT_PUBLIC_VAPID_PUBLIC_KEY}
ENV VAPID_PRIVATE_KEY=${VAPID_PRIVATE_KEY}
ENV BETTER_AUTH_SECRET=${BETTER_AUTH_SECRET}
ENV BETTER_AUTH_URL=${BETTER_AUTH_URL}
ENV CI=true
ENV NODE_OPTIONS="--max-old-space-size=3072"
COPY pnpm-lock.yaml pnpm-workspace.yaml package.json ./
RUN echo "Installing dependencies..." && pnpm install --frozen-lockfile
@@ -30,16 +33,25 @@ RUN echo "Installing dependencies..." && pnpm install --frozen-lockfile
# Copy source code
COPY . .
# Create minimal .env file for Next.js build
RUN cat > apps/web/.env.local << EOF
NEXT_PUBLIC_VAPID_PUBLIC_KEY=${NEXT_PUBLIC_VAPID_PUBLIC_KEY}
VAPID_PRIVATE_KEY=${VAPID_PRIVATE_KEY}
BETTER_AUTH_SECRET=${BETTER_AUTH_SECRET}
BETTER_AUTH_URL=${BETTER_AUTH_URL}
EOF
# Build the app with verbose output
RUN echo "Building application..." && pnpm build || (echo "Build failed, showing logs..." && cat /tmp/pnpm-*.log 2>/dev/null || true && exit 1)
RUN echo "Building application..." && pnpm build
# Production runtime
FROM base AS runtime
RUN apk add --no-cache libc6-compat postgresql-client
RUN apk add --no-cache libc6-compat postgresql-client curl
ENV NODE_ENV production
ENV NODE_OPTIONS="--max-old-space-size=2048"
# Copy production node_modules
COPY --from=deps /app/node_modules ./node_modules
# Copy production node_modules from builder stage
COPY --from=builder /app/node_modules ./node_modules
# Copy built Next.js app
COPY --from=builder /app/apps/web/.next/standalone ./
@@ -66,4 +78,7 @@ EXPOSE 3000
ENV PORT 3000
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
CMD curl -f http://localhost:3000/api/health || exit 1
CMD ["/app/entrypoint.sh"]