|
1 | 1 | # Based on https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile
|
2 |
| - |
3 | 2 | FROM node:20.5.0-alpine AS base
|
4 | 3 |
|
5 | 4 | # Install dependencies only when needed
|
6 |
| -FROM base AS builder |
| 5 | +FROM base AS deps |
7 | 6 | # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
|
8 | 7 | RUN apk add --no-cache libc6-compat
|
9 | 8 | WORKDIR /app
|
10 | 9 |
|
11 | 10 | <§yarn§># Yarn --------------------------------------------------------------------------
|
12 |
| -# NEW enable yarn 4.0.2 version and copy yarnrc.yml |
13 | 11 | RUN corepack enable
|
14 | 12 | COPY .yarn ./.yarn
|
15 | 13 | </§yarn§>
|
16 |
| -# Install dependencies based on the preferred package manager (NEW copy yarnrc.yml to the image) |
17 |
| -COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* .yarnrc.yml* ./ |
18 |
| - |
19 |
| -# use this line if you have dependencies that for some reason need global typescript |
20 |
| -# RUN npm install typescript -g |
21 | 14 |
|
| 15 | +# Install dependencies based on the preferred package manager |
| 16 | +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* .npmrc* .yarnrc.yml* ./ |
22 | 17 | RUN \
|
23 | 18 | if [ -f yarn.lock ]; then yarn --immutable; \
|
24 |
| - elif [ -f package-lock.json ]; then npm ci; \ |
| 19 | + elif [ -f package-lock.json ]; then npm ci --force; \ |
| 20 | + elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \ |
25 | 21 | else echo "Lockfile not found." && exit 1; \
|
26 | 22 | fi
|
27 | 23 |
|
28 |
| - |
29 | 24 | # Rebuild the source code only when needed
|
| 25 | +FROM base AS builder |
30 | 26 | WORKDIR /app
|
31 |
| -# COPY --from=builder /app/.yarn ./.yarn |
32 |
| -# COPY --from=builder /app/node_modules ./node_modules |
| 27 | + |
| 28 | +COPY --from=deps /app/node_modules ./node_modules |
33 | 29 | COPY . .
|
34 | 30 |
|
35 | 31 | # Next.js collects completely anonymous telemetry data about general usage.
|
36 | 32 | # Learn more here: https://nextjs.org/telemetry
|
37 |
| -# Uncomment the following line in case you want to disable telemetry during the build. |
38 |
| -ENV NEXT_TELEMETRY_DISABLED 1 |
| 33 | +ENV NEXT_TELEMETRY_DISABLED=1 |
| 34 | +ENV BUILD_STANDALONE=1 |
39 | 35 |
|
40 | 36 | RUN \
|
41 | 37 | if [ -f yarn.lock ]; then <§prisma§>yarn prisma generate && </§prisma§>yarn run build; \
|
|
47 | 43 | FROM base AS runner
|
48 | 44 | WORKDIR /app
|
49 | 45 |
|
50 |
| -ENV NODE_ENV production |
| 46 | +ENV NODE_ENV=production |
51 | 47 | # Uncomment the following line in case you want to disable telemetry during runtime.
|
52 |
| -ENV NEXT_TELEMETRY_DISABLED 1 |
| 48 | +ENV NEXT_TELEMETRY_DISABLED=1 |
53 | 49 |
|
54 | 50 | RUN addgroup --system --gid 1001 nodejs
|
55 | 51 | RUN adduser --system --uid 1001 nextjs
|
56 | 52 |
|
57 |
| -# adjust files to your package manager and packages |
58 |
| -COPY --from=builder /app/next.config.ts ./ |
59 |
| -COPY --from=builder /app/i18nConfig.js ./ |
60 |
| -COPY --from=builder /app/public ./public |
61 |
| -COPY --from=builder /app/.next ./.next |
62 |
| -COPY --from=builder /app/package.json ./package.json |
63 |
| -<§yarn§># Yarn -------------------------------------------------------------------------- |
64 |
| -COPY --from=builder /app/.yarn ./.yarn |
65 |
| -COPY --from=builder /app/yarn.lock ./yarn.lock |
66 |
| -COPY --from=builder /app/.yarnrc.yml ./.yarnrc.yml |
67 |
| -</§yarn§><§npm§># NPM --------------------------------------------------------------------------- |
68 |
| -COPY --from=builder /app/node_modules ./node_modules |
69 |
| -COPY --from=builder /app/package-lock.json ./package-lock.json |
70 |
| -</§npm§><§prisma§># Prisma ------------------------------------------------------------------------ |
71 |
| -COPY --from=builder /app/prisma ./prisma |
72 |
| -</§prisma§> |
73 |
| -# use this line if you have dependencies that for some reason need global typescript |
74 |
| -# RUN npm install typescript -g |
75 |
| - |
76 |
| -# Note rebuild again - this is to let the package manager rebuild binaries in the "runner" stage of the Dockerfile |
77 |
| -<§yarn§># We also have to remove unplugged, so that rebuilding happens and replaces the old binaries |
78 |
| -RUN rm -rf /app/.yarn/unplugged && yarn rebuild |
79 |
| -</§yarn§> |
80 |
| - |
81 | 53 | # Set the correct permission for prerender cache
|
82 |
| -RUN chown -R nextjs:nodejs /app/.next |
83 |
| -<§yarn§>RUN chown -R nextjs:nodejs /app/.yarn |
84 |
| -</§yarn§> |
| 54 | +RUN mkdir .next |
| 55 | +RUN chown -R nextjs:nodejs .next |
85 | 56 |
|
| 57 | +<§prisma§># Prisma ------------------------------------------------------------------------ |
| 58 | +COPY --from=builder /app/prisma ./prisma |
| 59 | +</§prisma§> |
86 | 60 | # Automatically leverage output traces to reduce image size
|
87 | 61 | # https://nextjs.org/docs/advanced-features/output-file-tracing
|
88 |
| -# COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ |
89 |
| -# COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static |
| 62 | +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ |
| 63 | +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static |
90 | 64 |
|
91 | 65 | USER nextjs
|
92 | 66 |
|
93 | 67 | EXPOSE 3000
|
94 | 68 |
|
95 |
| -ENV PORT 3000 |
96 |
| -# set hostname to localhost |
97 |
| -# ENV HOSTNAME "0.0.0.0" |
| 69 | +ENV PORT=3000 |
98 | 70 |
|
99 | 71 | # server.js is created by next build from the standalone output
|
100 | 72 | # https://nextjs.org/docs/pages/api-reference/next-config-js/output
|
101 |
| -<§yarn§>CMD ["yarn", "start"] |
102 |
| -</§yarn§><§npm§>CMD ["npm", "run", "start"] |
103 |
| -</§npm§> |
| 73 | +ENV HOSTNAME="0.0.0.0" |
| 74 | +CMD ["node", "server.js"] |
0 commit comments