From 8e0f5cdaa5f595cee556b05f54a48c2cb3d61a0b Mon Sep 17 00:00:00 2001 From: Mackie Date: Fri, 22 May 2026 19:50:42 +0800 Subject: [PATCH] it should work --- Dockerfile | 71 ++++++++++++++-------------------------------- docker-compose.yml | 30 ++++++++------------ next.config.ts | 17 ++++++----- 3 files changed, 44 insertions(+), 74 deletions(-) diff --git a/Dockerfile b/Dockerfile index 99e029a..1034b98 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,71 +1,44 @@ -# To use this Dockerfile, you have to set `output: 'standalone'` in your next.config.js file. -# From https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile +FROM node:20-alpine AS base -FROM node:22.17.0-alpine AS base - -# Install dependencies only when needed FROM base AS deps -# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. RUN apk add --no-cache libc6-compat WORKDIR /app +RUN corepack enable && corepack prepare pnpm@9 --activate +COPY package.json pnpm-lock.yaml ./ +RUN pnpm install --frozen-lockfile -# Install dependencies based on the preferred package manager -COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ -RUN \ - if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ - elif [ -f package-lock.json ]; then npm ci; \ - elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \ - else echo "Lockfile not found." && exit 1; \ - fi - - -# Rebuild the source code only when needed FROM base AS builder WORKDIR /app +RUN corepack enable && corepack prepare pnpm@9 --activate COPY --from=deps /app/node_modules ./node_modules COPY . . -# Next.js collects completely anonymous telemetry data about general usage. -# Learn more here: https://nextjs.org/telemetry -# Uncomment the following line in case you want to disable telemetry during the build. -# ENV NEXT_TELEMETRY_DISABLED 1 +ENV NEXT_TELEMETRY_DISABLED=1 +ENV DATABASE_URI="mongodb://localhost:27017/build-placeholder" +ENV PAYLOAD_SECRET="build-time-placeholder" +ENV NEXT_PUBLIC_SERVER_URL="http://localhost:3000" +ENV NODE_ENV=production -RUN \ - if [ -f yarn.lock ]; then yarn run build; \ - elif [ -f package-lock.json ]; then npm run build; \ - elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \ - else echo "Lockfile not found." && exit 1; \ - fi +RUN pnpm payload generate:types || echo "Skipped" +RUN pnpm next build --experimental-build-mode compile -# Production image, copy all the files and run next FROM base AS runner WORKDIR /app +ENV NODE_ENV=production +ENV NEXT_TELEMETRY_DISABLED=1 -ENV NODE_ENV production -# Uncomment the following line in case you want to disable telemetry during runtime. -# ENV NEXT_TELEMETRY_DISABLED 1 +RUN apk add --no-cache libc6-compat curl +RUN addgroup --system --gid 1001 nodejs && \ + adduser --system --uid 1001 nextjs -RUN addgroup --system --gid 1001 nodejs -RUN adduser --system --uid 1001 nextjs - -# Remove this line if you do not have this folder COPY --from=builder /app/public ./public - -# Set the correct permission for prerender cache -RUN mkdir .next -RUN chown nextjs:nodejs .next - -# Automatically leverage output traces to reduce image size -# https://nextjs.org/docs/advanced-features/output-file-tracing +RUN mkdir .next && chown nextjs:nodejs .next COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static USER nextjs +EXPOSE 3001 +ENV PORT=3001 +ENV HOSTNAME=0.0.0.0 -EXPOSE 3000 - -ENV PORT 3000 - -# server.js is created by next build from the standalone output -# https://nextjs.org/docs/pages/api-reference/next-config-js/output -CMD HOSTNAME="0.0.0.0" node server.js +CMD ["node", "server.js"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 4c9fc51..6d13164 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,29 +3,23 @@ version: '3' services: payload: image: node:18-alpine + pull_policy: always ports: - - '3000:3000' + - '3001:3000' volumes: - .:/home/node/app - node_modules:/home/node/app/node_modules working_dir: /home/node/app/ command: sh -c "yarn install && yarn dev" - depends_on: - - mongo - env_file: - - .env - - mongo: - image: mongo:latest - ports: - - '27017:27017' - command: - - --storageEngine=wiredTiger - volumes: - - data:/data/db - logging: - driver: none + environment: + - NODE_ENV=production + - DATABASE_URI=${DATABASE_URI} + - PAYLOAD_SECRET=${PAYLOAD_SECRET} + - NEXT_PUBLIC_SERVER_URL=${NEXT_PUBLIC_SERVER_URL} + - CRON_SECRET=${CRON_SECRET} + - PREVIEW_SECRET=${PREVIEW_SECRET} + - PAYLOAD_DROP_DATABASE=false + - PAYLOAD_SEED=false volumes: - data: - node_modules: + node_modules: \ No newline at end of file diff --git a/next.config.ts b/next.config.ts index 988368c..7d0c720 100644 --- a/next.config.ts +++ b/next.config.ts @@ -7,13 +7,9 @@ const __filename = fileURLToPath(import.meta.url) const dirname = path.dirname(__filename) import { redirects } from './redirects' -const NEXT_PUBLIC_SERVER_URL = process.env.VERCEL_PROJECT_PRODUCTION_URL - ? `https://${process.env.VERCEL_PROJECT_PRODUCTION_URL}` - : process.env.__NEXT_PRIVATE_ORIGIN || 'http://localhost:3000' +const NEXT_PUBLIC_SERVER_URL = process.env.NEXT_PUBLIC_SERVER_URL || 'http://localhost:3001' const nextConfig: NextConfig = { - // Temporarily required on Windows until Next.js fixes Turbopack Sass resolution. - // See: https://github.com/vercel/next.js/issues/86431 sassOptions: { loadPaths: ['./node_modules/@payloadcms/ui/dist/scss/'], }, @@ -25,7 +21,7 @@ const nextConfig: NextConfig = { ], qualities: [100], remotePatterns: [ - ...[NEXT_PUBLIC_SERVER_URL /* 'https://example.com' */].map((item) => { + ...[NEXT_PUBLIC_SERVER_URL].map((item) => { const url = new URL(item) return { @@ -42,8 +38,15 @@ const nextConfig: NextConfig = { '.mjs': ['.mts', '.mjs'], } + const replaceHash = (val: unknown) => + typeof val === 'string' ? val.replace('[chunkhash]', '[contenthash]') : val + + webpackConfig.output.filename = replaceHash(webpackConfig.output.filename) + webpackConfig.output.chunkFilename = replaceHash(webpackConfig.output.chunkFilename) + return webpackConfig }, + reactStrictMode: true, redirects, turbopack: { @@ -51,4 +54,4 @@ const nextConfig: NextConfig = { }, } -export default withPayload(nextConfig, { devBundleServerPackages: false }) +export default withPayload(nextConfig, { devBundleServerPackages: false }) \ No newline at end of file