memex-in commited on
Commit
457930f
·
verified ·
1 Parent(s): 98b06f9

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +48 -49
Dockerfile CHANGED
@@ -1,67 +1,66 @@
1
- # --- Stage 1: Builder ---
2
- # Use a Node.js image suitable for building (includes build tools if needed)
3
- FROM node:18-alpine AS builder
4
- RUN ls
5
- # Set working directory
6
- WORKDIR /root
7
- RUN ls
8
 
9
- RUN npm install
10
-
11
- # Copy package.json and package-lock.json (or yarn.lock/pnpm-lock.yaml)
12
- # Copying these separately allows Docker to cache this step if only code changes
13
- COPY package.json ./
14
- # If you are using yarn, uncomment the line below and comment out the npm line
15
- # COPY yarn.lock ./
16
- # If you are using pnpm, uncomment the line below and comment out the npm line
17
- # COPY pnpm-lock.yaml ./
18
- COPY package-lock.json ./ # Use package-lock.json if you use npm
19
 
 
 
 
 
 
20
 
21
- # Install dependencies including devDependencies for the build
22
- # Use --frozen-lockfile or --ci for deterministic builds if using npm or yarn v1
23
- RUN npm ci # Or `yarn install --frozen-lockfile` or `pnpm install --frozen-lockfile`
 
 
 
 
 
24
 
25
 
26
- # Copy the rest of the application code
 
 
 
27
  COPY . .
28
 
29
- # Build the Next.js application
30
- # NEXT_TELEMETRY_DISABLED=1 disables Next.js telemetry during the build
31
- RUN NEXT_TELEMETRY_DISABLED=1 npm run build
 
32
 
33
- # --- Stage 2: Runner ---
34
- # Use a smaller, production-focused Node.js image
35
- FROM node:18-alpine AS runner
 
 
 
36
 
37
- # Set working directory
 
38
  WORKDIR /app
39
 
40
- # Copy necessary files from the builder stage
41
- # Copy production dependencies
42
- COPY --from=builder /app/package.json ./
43
- COPY --from=builder /app/node_modules ./node_modules
44
- # Copy the build output (.next folder)
45
- COPY --from=builder /app/.next ./.next
46
- # Copy public assets
47
- COPY --from=builder /app/public ./public
48
- # Copy next.config.js if it exists
49
- COPY --from=builder /app/next.config.js ./next.config.js
50
 
 
51
 
52
- # Set the environment variable for the Next.js port (default is 3000)
53
- # Hugging Face Spaces typically exposes port 7860 for Gradio/Streamlit/etc,
54
- # but for general web apps (like Next.js via the static host option or if you set up a custom server),
55
- # it might expose port 3000 by default when detecting a standard Next.js app.
56
- # It's good practice to set this, but Spaces often handles port mapping.
57
- ENV PORT 3000
58
 
 
59
 
60
- # Expose the port the application will run on
61
  EXPOSE 3000
62
 
63
- # Disable Next.js telemetry at runtime
64
- ENV NEXT_TELEMETRY_DISABLED 1
65
 
66
- # Command to run the production Next.js server
67
- CMD ["npm", "start"]
 
 
 
1
+ # syntax=docker.io/docker/dockerfile:1
 
 
 
 
 
 
2
 
3
+ FROM node:18-alpine AS base
 
 
 
 
 
 
 
 
 
4
 
5
+ # Install dependencies only when needed
6
+ FROM base AS deps
7
+ # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
8
+ RUN apk add --no-cache libc6-compat
9
+ WORKDIR /app
10
 
11
+ # Install dependencies based on the preferred package manager
12
+ COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* .npmrc* ./
13
+ RUN \
14
+ if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
15
+ elif [ -f package-lock.json ]; then npm ci; \
16
+ elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
17
+ else echo "Lockfile not found." && exit 1; \
18
+ fi
19
 
20
 
21
+ # Rebuild the source code only when needed
22
+ FROM base AS builder
23
+ WORKDIR /app
24
+ COPY --from=deps /app/node_modules ./node_modules
25
  COPY . .
26
 
27
+ # Next.js collects completely anonymous telemetry data about general usage.
28
+ # Learn more here: https://nextjs.org/telemetry
29
+ # Uncomment the following line in case you want to disable telemetry during the build.
30
+ # ENV NEXT_TELEMETRY_DISABLED=1
31
 
32
+ RUN \
33
+ if [ -f yarn.lock ]; then yarn run build; \
34
+ elif [ -f package-lock.json ]; then npm run build; \
35
+ elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
36
+ else echo "Lockfile not found." && exit 1; \
37
+ fi
38
 
39
+ # Production image, copy all the files and run next
40
+ FROM base AS runner
41
  WORKDIR /app
42
 
43
+ ENV NODE_ENV=production
44
+ # Uncomment the following line in case you want to disable telemetry during runtime.
45
+ # ENV NEXT_TELEMETRY_DISABLED=1
46
+
47
+ RUN addgroup --system --gid 1001 nodejs
48
+ RUN adduser --system --uid 1001 nextjs
 
 
 
 
49
 
50
+ COPY --from=builder /app/public ./public
51
 
52
+ # Automatically leverage output traces to reduce image size
53
+ # https://nextjs.org/docs/advanced-features/output-file-tracing
54
+ COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
55
+ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
 
 
56
 
57
+ USER nextjs
58
 
 
59
  EXPOSE 3000
60
 
61
+ ENV PORT=3000
 
62
 
63
+ # server.js is created by next build from the standalone output
64
+ # https://nextjs.org/docs/pages/api-reference/config/next-config-js/output
65
+ ENV HOSTNAME="0.0.0.0"
66
+ CMD ["node", "server.js"]