-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDockerfile
75 lines (58 loc) · 1.83 KB
/
Dockerfile
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
63
64
65
66
67
68
69
70
71
72
73
74
75
# Base image
FROM node:20.14-alpine AS base
# Install dependencies only when needed
FROM base AS deps
# Install compatibility libraries
RUN apk add --no-cache libc6-compat
WORKDIR /app
# Copy dependency files
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
# Install dependencies based on lock files
RUN \
if [ -f yarn.lock ]; then \
corepack enable && \
yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then \
npm config set registry https://registry.npmmirror.com && \
npm ci; \
elif [ -f pnpm-lock.yaml ]; then \
corepack enable pnpm && \
pnpm config set registry https://registry.npmmirror.com && \
pnpm i --frozen-lockfile; \
else \
echo "No lock file found." && exit 1; \
fi
# Rebuild source code only when needed
FROM base AS builder
WORKDIR /app
# Copy dependencies and source code
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Build project based on build mode
RUN corepack enable pnpm && pnpm run build;
# Production image, copy all files and run Next.js
FROM base AS runner
WORKDIR /app
# Create a non-root user
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# Copy static files
COPY --from=builder /app/public ./public
# Set permissions for prerendered cache
RUN mkdir .next
RUN chown nextjs:nodejs .next
# Copy build artefacts
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
# Copy .env file
COPY --chown=nextjs:nodejs .env .env
# Create persistent data directory and set permissions
RUN mkdir -p /app/shared && chmod -R 777 /app/shared
# Switch to non-root user
USER nextjs
# Expose port
EXPOSE 3000
# Set environment variables
ENV PORT=3000
# Start command
CMD HOSTNAME="0.0.0.0" node server.js