Next.js / Dockerfile
memex-in's picture
Update Dockerfile
b130d3c verified
raw
history blame
2.23 kB
# --- Stage 1: Builder ---
# Use a Node.js image suitable for building (includes build tools if needed)
FROM node:18-alpine AS builder
RUN ls
# Set working directory
WORKDIR /app
RUN npm install
# Copy package.json and package-lock.json (or yarn.lock/pnpm-lock.yaml)
# Copying these separately allows Docker to cache this step if only code changes
COPY package.json ./
# If you are using yarn, uncomment the line below and comment out the npm line
# COPY yarn.lock ./
# If you are using pnpm, uncomment the line below and comment out the npm line
# COPY pnpm-lock.yaml ./
COPY package-lock.json ./ # Use package-lock.json if you use npm
# Install dependencies including devDependencies for the build
# Use --frozen-lockfile or --ci for deterministic builds if using npm or yarn v1
RUN npm ci # Or `yarn install --frozen-lockfile` or `pnpm install --frozen-lockfile`
# Copy the rest of the application code
COPY . .
# Build the Next.js application
# NEXT_TELEMETRY_DISABLED=1 disables Next.js telemetry during the build
RUN NEXT_TELEMETRY_DISABLED=1 npm run build
# --- Stage 2: Runner ---
# Use a smaller, production-focused Node.js image
FROM node:18-alpine AS runner
# Set working directory
WORKDIR /app
# Copy necessary files from the builder stage
# Copy production dependencies
COPY --from=builder /app/package.json ./
COPY --from=builder /app/node_modules ./node_modules
# Copy the build output (.next folder)
COPY --from=builder /app/.next ./.next
# Copy public assets
COPY --from=builder /app/public ./public
# Copy next.config.js if it exists
COPY --from=builder /app/next.config.js ./next.config.js
# Set the environment variable for the Next.js port (default is 3000)
# Hugging Face Spaces typically exposes port 7860 for Gradio/Streamlit/etc,
# but for general web apps (like Next.js via the static host option or if you set up a custom server),
# it might expose port 3000 by default when detecting a standard Next.js app.
# It's good practice to set this, but Spaces often handles port mapping.
ENV PORT 3000
# Expose the port the application will run on
EXPOSE 3000
# Disable Next.js telemetry at runtime
ENV NEXT_TELEMETRY_DISABLED 1
# Command to run the production Next.js server
CMD ["npm", "start"]