From bb972e81ae98820cc351e22445a7ca3aa48a7404 Mon Sep 17 00:00:00 2001 From: Syasya Date: Fri, 22 Aug 2025 16:45:14 +0800 Subject: [PATCH] update dockerfile --- .env.example | 2 +- Dockerfile | 64 ++++++++++++++++++++++++++++++++++++-------------- next.config.js | 1 + 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/.env.example b/.env.example index b062194..2cd6698 100644 --- a/.env.example +++ b/.env.example @@ -2,5 +2,5 @@ FASTAPI_URL="http://localhost:8000" INTERNAL_API_BASE_URL="http://localhost:3005" -DATABASE_URL="postgresql://postgres:root@localhost:5432/rooftop?schema=public" +DATABASE_URL="postgresql://user:pass@localhost:5432/rooftop?schema=public" JWT_SECRET="secret_key" diff --git a/Dockerfile b/Dockerfile index 11139cc..df7d547 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,26 +1,54 @@ -FROM node:18 +# syntax=docker/dockerfile:1 +# ---- Build stage ---- +ARG NODE_VERSION=20 +FROM node:${NODE_VERSION}-slim AS builder WORKDIR /app + +ENV NEXT_TELEMETRY_DISABLED=1 + +# Build-time public env hook +ARG NEXT_PUBLIC_FASTAPI_URL +ENV NEXT_PUBLIC_FASTAPI_URL=${NEXT_PUBLIC_FASTAPI_URL} + +# 1) Install deps with caching +COPY package.json package-lock.json* ./ +RUN npm ci + +# 2) Copy source COPY . . -# Accept build-time arguments -ARG FASTAPI_URL -ARG INTERNAL_API_BASE_URL -ARG DATABASE_URL -ARG JWT_SECRET +# 3) Generate Prisma client (safe no-op if unused) +RUN npx prisma generate || true -# Assign them to environment variables inside the image -ENV FASTAPI_URL=$FASTAPI_URL -ENV INTERNAL_API_BASE_URL=$INTERNAL_API_BASE_URL -ENV DATABASE_URL=$DATABASE_URL -ENV JWT_SECRET=$JWT_SECRET - -RUN npm install --force - -# Build Next.js app -RUN npx prisma generate +# 4) Build Next.js (requires next.config.js => output: 'standalone') RUN npm run build -EXPOSE 3005 +# ---- Runtime stage ---- +FROM node:${NODE_VERSION}-slim AS runner +WORKDIR /app + +ENV NODE_ENV=production \ + NEXT_TELEMETRY_DISABLED=1 \ + HOSTNAME=0.0.0.0 \ + PORT=3005 + +# Minimal server & assets +COPY --from=builder /app/.next/standalone ./ +COPY --from=builder /app/.next/static ./.next/static +COPY --from=builder /app/public ./public +COPY --from=builder /app/prisma ./prisma + +# Optional (only if Prisma engine errors show up): +# COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma +# COPY --from=builder /app/node_modules/@prisma ./node_modules/@prisma + +USER node +EXPOSE 3005 + +# (Optional) healthcheck +# HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \ +# CMD node -e "require('http').get('http://127.0.0.1:'+(process.env.PORT||3005)+'/_next/static/webpack/')" + +CMD ["node", "server.js"] -CMD ["npm", "start"] \ No newline at end of file diff --git a/next.config.js b/next.config.js index 0969773..5e064b1 100644 --- a/next.config.js +++ b/next.config.js @@ -1,5 +1,6 @@ /** @type {import('next').NextConfig} */ const nextConfig = { + output: 'standalone', reactStrictMode: false, swcMinify: true, eslint: {