|
1 | | -FROM node:18-alpine |
| 1 | +FROM node:18-alpine AS base |
2 | 2 |
|
3 | | -RUN mkdir -p /usr/src/app |
| 3 | +# Step 1. Rebuild the source code only when needed |
| 4 | +FROM base AS builder |
4 | 5 |
|
5 | | -WORKDIR /usr/src/app |
| 6 | +WORKDIR /app |
6 | 7 |
|
7 | | -COPY package.json /usr/src/app/ |
| 8 | +# Install dependencies based on the preferred package manager |
| 9 | +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ |
| 10 | +# Omit --production flag for TypeScript devDependencies |
| 11 | +RUN \ |
| 12 | + if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ |
| 13 | + elif [ -f package-lock.json ]; then npm ci; \ |
| 14 | + elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i; \ |
| 15 | + # Allow install without lockfile, so example works even without Node.js installed locally |
| 16 | + else echo "Warning: Lockfile not found. It is recommended to commit lockfiles to version control." && yarn install; \ |
| 17 | + fi |
8 | 18 |
|
9 | | -RUN npm install -g next |
| 19 | +COPY . . |
| 20 | +COPY public ./public |
| 21 | +COPY next.config.js . |
| 22 | +COPY tsconfig.json . |
10 | 23 |
|
11 | | -RUN npm install |
| 24 | +# Environment variables must be present at build time |
| 25 | +# https://github.com/vercel/next.js/discussions/14030 |
| 26 | +ARG ENV_VARIABLE |
| 27 | +ENV ENV_VARIABLE=${ENV_VARIABLE} |
| 28 | +ARG NEXT_PUBLIC_ENV_VARIABLE |
| 29 | +ENV NEXT_PUBLIC_ENV_VARIABLE=${NEXT_PUBLIC_ENV_VARIABLE} |
12 | 30 |
|
13 | | -COPY . /usr/src/app |
| 31 | +# Next.js collects completely anonymous telemetry data about general usage. Learn more here: https://nextjs.org/telemetry |
| 32 | +# Uncomment the following line to disable telemetry at build time |
| 33 | +# ENV NEXT_TELEMETRY_DISABLED 1 |
14 | 34 |
|
15 | | -RUN npm run build |
| 35 | +# Build Next.js based on the preferred package manager |
| 36 | +RUN \ |
| 37 | + if [ -f yarn.lock ]; then yarn build; \ |
| 38 | + elif [ -f package-lock.json ]; then npm run build; \ |
| 39 | + elif [ -f pnpm-lock.yaml ]; then pnpm build; \ |
| 40 | + else yarn build; \ |
| 41 | + fi |
16 | 42 |
|
17 | | -COPY --chown=10101 ./public /usr/src/app/public |
| 43 | +# Note: It is not necessary to add an intermediate step that does a full copy of `node_modules` here |
18 | 44 |
|
19 | | -ENV DB_LOCAL_URL=mongodb://mongo:27017/denizpaz |
| 45 | +# Step 2. Production image, copy all the files and run next |
| 46 | +FROM base AS runner |
20 | 47 |
|
21 | | -EXPOSE 3000 |
| 48 | +WORKDIR /app |
| 49 | + |
| 50 | +# Don't run production as root |
| 51 | +RUN addgroup --system --gid 1001 nodejs |
| 52 | +RUN adduser --system --uid 1001 nextjs |
| 53 | +USER nextjs |
| 54 | + |
| 55 | +COPY --from=builder /app/public ./public |
| 56 | + |
| 57 | +# Automatically leverage output traces to reduce image size |
| 58 | +# https://nextjs.org/docs/advanced-features/output-file-tracing |
| 59 | +# COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ |
| 60 | +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static |
| 61 | + |
| 62 | +# Environment variables must be redefined at run time |
| 63 | +ARG ENV_VARIABLE |
| 64 | +ENV ENV_VARIABLE=${ENV_VARIABLE} |
| 65 | +ARG NEXT_PUBLIC_ENV_VARIABLE |
| 66 | +ENV NEXT_PUBLIC_ENV_VARIABLE=${NEXT_PUBLIC_ENV_VARIABLE} |
| 67 | + |
| 68 | +# Uncomment the following line to disable telemetry at run time |
| 69 | +# ENV NEXT_TELEMETRY_DISABLED 1 |
| 70 | + |
| 71 | +# Note: Don't expose ports here, Compose will handle that for us |
22 | 72 |
|
23 | 73 | CMD npm run launch |
0 commit comments