Dockerfile 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. # Install dependencies based on the preferred package manager
  7. COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
  8. RUN \
  9. if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
  10. elif [ -f package-lock.json ]; then npm ci; \
  11. elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i; \
  12. else echo "Lockfile not found." && exit 1; \
  13. fi
  14. # Rebuild the source code only when needed
  15. FROM node:alpine AS builder
  16. WORKDIR /app
  17. COPY --from=deps /app/node_modules ./node_modules
  18. COPY . .
  19. # Next.js collects completely anonymous telemetry data about general usage.
  20. # Learn more here: https://nextjs.org/telemetry
  21. # Uncomment the following line in case you want to disable telemetry during the build.
  22. # ENV NEXT_TELEMETRY_DISABLED 1
  23. RUN yarn build
  24. # If using npm comment out above and use below instead
  25. # RUN npm run build
  26. # Production image, copy all the files and run next
  27. FROM node:alpine AS runner
  28. WORKDIR /app
  29. ENV NODE_ENV production
  30. # Uncomment the following line in case you want to disable telemetry during runtime.
  31. # ENV NEXT_TELEMETRY_DISABLED 1
  32. RUN addgroup --system --gid 1001 nodejs
  33. RUN adduser --system --uid 1001 nextjs
  34. # You only need to copy next.config.js if you are NOT using the default configuration
  35. # COPY --from=builder /app/next.config.js ./
  36. COPY --from=builder /app/public ./public
  37. COPY --from=builder /app/package.json ./package.json
  38. # Automatically leverage output traces to reduce image size
  39. # https://nextjs.org/docs/advanced-features/output-file-tracing
  40. COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
  41. COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
  42. USER nextjs
  43. EXPOSE 3000
  44. ENV PORT 3000
  45. # Add MongoDB support if needed
  46. RUN apk add --no-cache mongodb-tools
  47. # Add health check
  48. HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
  49. CMD node -e "require('http').get('http://localhost:3000', (r) => {if (r.statusCode !== 200) throw new Error()})"
  50. # Set MongoDB URI (adjust as needed)
  51. ENV MONGODB_URI mongodb://mongodb:27017/mydatabase
  52. CMD ["node", "server.js"]