Dockerfile.prod 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Install dependencies only when needed
  2. FROM node:alpine AS deps
  3. # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
  4. RUN apk add --no-cache libc6-compat
  5. WORKDIR /app
  6. COPY package.json yarn.lock ./
  7. RUN yarn install --frozen-lockfile
  8. # Rebuild the source code only when needed
  9. FROM node:alpine AS builder
  10. WORKDIR /app
  11. # 添加这行
  12. ARG MONGODB_URI
  13. ENV MONGODB_URI=$MONGODB_URI
  14. COPY . .
  15. COPY --from=deps /app/node_modules ./node_modules
  16. RUN yarn build && yarn install --production --ignore-scripts --prefer-offline
  17. # Production image, copy all the files and run next
  18. FROM node:alpine AS runner
  19. WORKDIR /app
  20. ENV NODE_ENV production
  21. RUN addgroup -g 1001 -S nodejs
  22. RUN adduser -S nextjs -u 1001
  23. # You only need to copy next.config.js if you are NOT using the default configuration
  24. # COPY --from=builder /app/next.config.js ./
  25. COPY --from=builder /app/public ./public
  26. COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
  27. COPY --from=builder /app/node_modules ./node_modules
  28. COPY --from=builder /app/package.json ./package.json
  29. USER nextjs
  30. EXPOSE 3000
  31. ENV PORT 3000
  32. # Next.js collects completely anonymous telemetry data about general usage.
  33. # Learn more here: https://nextjs.org/telemetry
  34. # Uncomment the following line in case you want to disable telemetry.
  35. # ENV NEXT_TELEMETRY_DISABLED 1
  36. CMD ["node_modules/.bin/next", "start"]