Praneeth Yerrapragada commited on
Commit
064eaa5
1 Parent(s): 51cce4f

build: fix docker file for nextjs

Browse files

Copied from https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile

Files changed (2) hide show
  1. Dockerfile +26 -21
  2. next.config.json +3 -7
Dockerfile CHANGED
@@ -1,34 +1,38 @@
1
- FROM node:20-alpine as base
2
 
3
  # Install dependencies only when needed
4
  FROM base AS deps
 
 
5
  WORKDIR /app
6
 
7
  # Install dependencies based on the preferred package manager
8
- COPY --link package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
9
  RUN \
10
  if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
11
  elif [ -f package-lock.json ]; then npm ci; \
12
- elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
13
  else echo "Lockfile not found." && exit 1; \
14
  fi
15
 
 
16
  # Rebuild the source code only when needed
17
  FROM base AS builder
18
  WORKDIR /app
19
- COPY --from=deps --link /app/node_modules ./node_modules
20
- COPY --link . .
21
 
22
  # Next.js collects completely anonymous telemetry data about general usage.
23
  # Learn more here: https://nextjs.org/telemetry
24
  # Uncomment the following line in case you want to disable telemetry during the build.
25
  # ENV NEXT_TELEMETRY_DISABLED 1
26
 
27
- # Build the application
28
- RUN npm run build
29
-
30
- # If using yarn comment out above and use below instead
31
- # RUN yarn build
 
32
 
33
  # Production image, copy all the files and run next
34
  FROM base AS runner
@@ -38,25 +42,26 @@ ENV NODE_ENV production
38
  # Uncomment the following line in case you want to disable telemetry during runtime.
39
  # ENV NEXT_TELEMETRY_DISABLED 1
40
 
41
- RUN \
42
- addgroup --system --gid 1001 nodejs; \
43
- adduser --system --uid 1001 nextjs
44
 
45
- COPY --from=builder --link /app/public ./public
 
 
 
 
46
 
47
  # Automatically leverage output traces to reduce image size
48
  # https://nextjs.org/docs/advanced-features/output-file-tracing
49
- COPY --from=builder --link --chown=1001:1001 /app/.next/standalone ./
50
- COPY --from=builder --link --chown=1001:1001 /app/.next/static ./.next/static
51
 
52
  USER nextjs
53
 
54
  EXPOSE 3000
55
 
56
  ENV PORT 3000
57
- ENV HOSTNAME 0.0.0.0
58
-
59
- # ====================================
60
- FROM base as release
61
 
62
- CMD ["npm", "run", "start"]
 
 
 
1
+ FROM node:20-alpine AS base
2
 
3
  # 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
  WORKDIR /app
8
 
9
  # Install dependencies based on the preferred package manager
10
+ COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
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 corepack enable pnpm && pnpm i --frozen-lockfile; \
15
  else echo "Lockfile not found." && exit 1; \
16
  fi
17
 
18
+
19
  # Rebuild the source code only when needed
20
  FROM base AS builder
21
  WORKDIR /app
22
+ COPY --from=deps /app/node_modules ./node_modules
23
+ COPY . .
24
 
25
  # Next.js collects completely anonymous telemetry data about general usage.
26
  # Learn more here: https://nextjs.org/telemetry
27
  # Uncomment the following line in case you want to disable telemetry during the build.
28
  # ENV NEXT_TELEMETRY_DISABLED 1
29
 
30
+ RUN \
31
+ if [ -f yarn.lock ]; then yarn run build; \
32
+ elif [ -f package-lock.json ]; then npm run build; \
33
+ elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
34
+ else echo "Lockfile not found." && exit 1; \
35
+ fi
36
 
37
  # Production image, copy all the files and run next
38
  FROM base AS runner
 
42
  # Uncomment the following line in case you want to disable telemetry during runtime.
43
  # ENV NEXT_TELEMETRY_DISABLED 1
44
 
45
+ RUN addgroup --system --gid 1001 nodejs
46
+ RUN adduser --system --uid 1001 nextjs
 
47
 
48
+ COPY --from=builder /app/public ./public
49
+
50
+ # Set the correct permission for prerender cache
51
+ RUN mkdir .next
52
+ RUN chown nextjs:nodejs .next
53
 
54
  # Automatically leverage output traces to reduce image size
55
  # https://nextjs.org/docs/advanced-features/output-file-tracing
56
+ COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
57
+ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
58
 
59
  USER nextjs
60
 
61
  EXPOSE 3000
62
 
63
  ENV PORT 3000
 
 
 
 
64
 
65
+ # server.js is created by next build from the standalone output
66
+ # https://nextjs.org/docs/pages/api-reference/next-config-js/output
67
+ CMD HOSTNAME="0.0.0.0" node server.js
next.config.json CHANGED
@@ -1,13 +1,9 @@
1
  {
2
  "experimental": {
3
  "outputFileTracingIncludes": {
4
- "/*": [
5
- "./cache/**/*"
6
- ]
7
  },
8
- "serverComponentsExternalPackages": [
9
- "sharp",
10
- "onnxruntime-node"
11
- ]
12
  }
13
  }
 
1
  {
2
  "experimental": {
3
  "outputFileTracingIncludes": {
4
+ "/*": ["./cache/**/*"]
 
 
5
  },
6
+ "serverComponentsExternalPackages": ["sharp", "onnxruntime-node"],
7
+ "output": "standalone"
 
 
8
  }
9
  }