Skip to content

Instantly share code, notes, and snippets.

@cyberinferno
Created January 13, 2022 09:39
Show Gist options
  • Save cyberinferno/a90be0ff1deb8f1dcdfab8771071a51a to your computer and use it in GitHub Desktop.
Save cyberinferno/a90be0ff1deb8f1dcdfab8771071a51a to your computer and use it in GitHub Desktop.
Working NextJS Dockerfile
# Install dependencies only when needed
FROM node:16-alpine 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
COPY package*.json ./
RUN npm ci --only=production
# Rebuild the source code only when needed
FROM node:16-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ARG NEXT_PUBLIC_API_URL="http://api.alpha.intsysbak.1.k8.yourstage.rocks:31788/"
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL
ARG NEXT_PUBLIC_FRONTEND_URL="http://alpha.intsysbak.1.k8.yourstage.rocks:31788/"
ENV NEXT_PUBLIC_FRONTEND_URL=$NEXT_PUBLIC_FRONTEND_URL
RUN npm run build
# Production image, copy all the files and run next
FROM node:16-alpine AS runner
WORKDIR /app
ENV NODE_ENV production
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
# You only need to copy next.config.js if you are NOT using the default configuration
COPY --from=builder /app/next.config.js ./next.config.js
COPY --from=builder /app/next-i18next.config.js ./next-i18next.config.js
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
USER nextjs
EXPOSE 5000
ENV PORT 5000
# Disable NextJS telemetry
ENV NEXT_TELEMETRY_DISABLED 1
CMD ["npm", "run", "start"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment