|
| 1 | +FROM node:18-alpine AS base |
| 2 | + |
| 3 | +# 1. Install dependencies only when needed |
| 4 | +FROM base AS deps |
| 5 | +# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. |
| 6 | +RUN apk add --no-cache libc6-compat |
| 7 | + |
| 8 | +# Set the working directory |
| 9 | +WORKDIR /app |
| 10 | + |
| 11 | +# Copy package.json and package-lock.json |
| 12 | +COPY package.json ./ |
| 13 | +COPY package-lock.json ./ |
| 14 | + |
| 15 | +RUN npm install |
| 16 | + |
| 17 | +# 2. Rebuild the source code only when needed |
| 18 | +FROM base AS builder |
| 19 | +WORKDIR /app |
| 20 | +COPY --from=deps /app/node_modules ./node_modules |
| 21 | +COPY . . |
| 22 | +# This will do the trick, use the corresponding env file for each environment. |
| 23 | +COPY .env .env |
| 24 | +RUN npm run build |
| 25 | + |
| 26 | +# 3. Production image, copy all the files and run next |
| 27 | +FROM base AS runner |
| 28 | +WORKDIR /app |
| 29 | + |
| 30 | +ENV NODE_ENV=production |
| 31 | + |
| 32 | +RUN addgroup -g 1001 -S nodejs |
| 33 | +RUN adduser -S nextjs -u 1001 |
| 34 | + |
| 35 | +COPY --from=builder /app/public ./public |
| 36 | + |
| 37 | +# Automatically leverage output traces to reduce image size |
| 38 | +# https://nextjs.org/docs/advanced-features/output-file-tracing |
| 39 | +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ |
| 40 | +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static |
| 41 | + |
| 42 | +COPY --from=builder /app/package.json ./ |
| 43 | + |
| 44 | +USER nextjs |
| 45 | + |
| 46 | +# Expose the port Next.js runs on |
| 47 | +EXPOSE 3000 |
| 48 | + |
| 49 | +ENV PORT=3000 |
| 50 | +ENV HOSTNAME=0.0.0.0 |
| 51 | + |
| 52 | +# Run the app using JSON array notation |
| 53 | +CMD ["node", "server.js"] |
| 54 | +#CMD HOSTNAME="0.0.0.0" node server.js |
0 commit comments