-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
62 lines (44 loc) · 1.9 KB
/
Copy pathDockerfile
File metadata and controls
62 lines (44 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Dockerfile
# 1. Builder stage: Builds the Next.js application
FROM node:20-alpine AS builder
WORKDIR /app
# Copy only package.json to ensure fresh resolution based on it
COPY package.json ./
# Remove any existing lock file and node_modules to ensure clean install from package.json
RUN rm -f package-lock.json* && rm -rf node_modules
# Install dependencies based purely on package.json
RUN npm install --legacy-peer-deps
# Copy the rest of the application source code
# The .dockerignore file will prevent unnecessary files from being copied
COPY . .
# Set environment variable to disable Next.js telemetry during the build
ENV NEXT_TELEMETRY_DISABLED 1
# Build the Next.js application
RUN npm run build
# 2. Runner stage: Runs the built Next.js application
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV production
# Uncomment the following line to disable telemetry during runtime if desired.
# ENV NEXT_TELEMETRY_DISABLED 1
# Create a non-root user and group for security best practices
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# Copy built assets from the builder stage
# These include the .next folder (production build), public assets, and node_modules
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
# If you have a next.config.js, ensure it's copied as well:
# COPY --from=builder /app/next.config.js ./next.config.js
# Create data directory and set permissions for SQLite DB
RUN mkdir -p /app/data && chown -R nextjs:nodejs /app/data
# Set the user to the non-root user created above
USER nextjs
# Expose the port the Next.js application runs on (default is 3000)
EXPOSE 3000
# Set the PORT environment variable (Next.js will respect this)
ENV PORT 3000
# Command to start the Next.js production server
CMD ["npm", "run", "start"]