Skip to content
This repository was archived by the owner on Jun 28, 2025. It is now read-only.

Commit bdcc179

Browse files
committed
ci: fix web production Dockerfile
1 parent 6a90ab9 commit bdcc179

File tree

1 file changed

+45
-44
lines changed

1 file changed

+45
-44
lines changed

web/Dockerfile.prod

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,67 @@
11
FROM node:18-alpine AS base
22

3-
# Step 1. Rebuild the source code only when needed
4-
FROM base AS builder
5-
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
67
WORKDIR /app
78

89
# Install dependencies based on the preferred package manager
910
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
10-
# Omit --production flag for TypeScript devDependencies
1111
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 yarn global add pnpm && pnpm i; \
15-
# Allow install without lockfile, so example works even without Node.js installed locally
16-
else echo "Warning: Lockfile not found. It is recommended to commit lockfiles to version control." && yarn install; \
17-
fi
18-
19-
COPY src ./src
20-
COPY public ./public
21-
COPY next.config.js .
22-
COPY tsconfig.json .
23-
24-
# Environment variables must be present at build time
25-
# https://github.com/vercel/next.js/discussions/14030
26-
ARG ENV_VARIABLE
27-
ENV ENV_VARIABLE=${ENV_VARIABLE}
28-
ARG NEXT_PUBLIC_ENV_VARIABLE
29-
ENV NEXT_PUBLIC_ENV_VARIABLE=${NEXT_PUBLIC_ENV_VARIABLE}
30-
31-
ENV NEXT_TELEMETRY_DISABLED 1
32-
33-
# Build Next.js based on the preferred package manager
34-
RUN \
35-
if [ -f yarn.lock ]; then yarn build; \
36-
elif [ -f package-lock.json ]; then npm run build; \
37-
elif [ -f pnpm-lock.yaml ]; then pnpm build; \
38-
else yarn build; \
39-
fi
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
4017

4118

42-
# Step 2. Production image, copy all the files and run next
43-
FROM base AS runner
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
4429

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
4539
WORKDIR /app
4640

41+
ENV NODE_ENV production
42+
# Uncomment the following line in case you want to disable telemetry during runtime.
43+
# ENV NEXT_TELEMETRY_DISABLED 1
44+
4745
RUN addgroup --system --gid 1001 nodejs
4846
RUN adduser --system --uid 1001 nextjs
49-
USER nextjs
5047

5148
COPY --from=builder /app/public ./public
5249

53-
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
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 ./
5457
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
5558

56-
# Environment variables must be redefined at run time
57-
ARG ENV_VARIABLE
58-
ENV ENV_VARIABLE=${ENV_VARIABLE}
59-
ARG NEXT_PUBLIC_ENV_VARIABLE
60-
ENV NEXT_PUBLIC_ENV_VARIABLE=${NEXT_PUBLIC_ENV_VARIABLE}
59+
USER nextjs
6160

62-
# Uncomment the following line to disable telemetry at run time
63-
ENV NEXT_TELEMETRY_DISABLED 1
61+
EXPOSE 3000
6462

63+
ENV PORT 3000
6564

66-
CMD ["node", "server.js"]
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

0 commit comments

Comments
 (0)