Skip to content

Commit 548ef12

Browse files
committed
fix(frontend): Dockerfile
1 parent 46bf379 commit 548ef12

File tree

1 file changed

+37
-46
lines changed

1 file changed

+37
-46
lines changed

frontend/Dockerfile

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +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 \
1212
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
1313
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; \
14+
elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
15+
else echo "Lockfile not found." && exit 1; \
1716
fi
1817

19-
COPY src ./src
20-
COPY public ./public
21-
COPY next.config.js \
22-
postcss.config.js \
23-
tailwind.config.ts \
24-
tsconfig.json \
25-
./
26-
27-
# Environment variables must be present at build time
28-
# https://github.com/vercel/next.js/discussions/14030
29-
ARG ENV_VARIABLE
30-
ENV ENV_VARIABLE=${ENV_VARIABLE}
31-
ARG NEXT_PUBLIC_ENV_VARIABLE
32-
ENV NEXT_PUBLIC_ENV_VARIABLE=${NEXT_PUBLIC_ENV_VARIABLE}
33-
34-
# Next.js collects completely anonymous telemetry data about general usage. Learn more here: https://nextjs.org/telemetry
35-
# Uncomment the following line to disable telemetry at build time
36-
ENV NEXT_TELEMETRY_DISABLED 1
37-
38-
# Build Next.js based on the preferred package manager
39-
RUN \
40-
if [ -f yarn.lock ]; then yarn build; \
41-
elif [ -f package-lock.json ]; then npm run build; \
42-
elif [ -f pnpm-lock.yaml ]; then pnpm build; \
43-
else yarn build; \
44-
fi
4518

46-
# Note: It is not necessary to add an intermediate step that does a full copy of `node_modules` here
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 . .
4724

48-
# Step 2. Production image, copy all the files and run next
49-
FROM base AS runner
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
29+
30+
RUN yarn build
5031

32+
# If using npm comment out above and use below instead
33+
# RUN npm run build
34+
35+
# Production image, copy all the files and run next
36+
FROM base AS runner
5137
WORKDIR /app
5238

53-
# Don't run production as root
39+
ENV NODE_ENV production
40+
# Uncomment the following line in case you want to disable telemetry during runtime.
41+
# ENV NEXT_TELEMETRY_DISABLED 1
42+
5443
RUN addgroup --system --gid 1001 nodejs
5544
RUN adduser --system --uid 1001 nextjs
56-
USER nextjs
5745

5846
COPY --from=builder /app/public ./public
5947

48+
# Set the correct permission for prerender cache
49+
RUN mkdir .next
50+
RUN chown nextjs:nodejs .next
51+
6052
# Automatically leverage output traces to reduce image size
6153
# https://nextjs.org/docs/advanced-features/output-file-tracing
6254
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
6355
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
6456

65-
# Environment variables must be redefined at run time
66-
ARG ENV_VARIABLE
67-
ENV ENV_VARIABLE=${ENV_VARIABLE}
68-
ARG NEXT_PUBLIC_ENV_VARIABLE
69-
ENV NEXT_PUBLIC_ENV_VARIABLE=${NEXT_PUBLIC_ENV_VARIABLE}
57+
USER nextjs
7058

71-
# Uncomment the following line to disable telemetry at run time
72-
# ENV NEXT_TELEMETRY_DISABLED 1
59+
EXPOSE 3000
7360

74-
# Note: Don't expose ports here, Compose will handle that for us
61+
ENV PORT 3000
62+
# set hostname to localhost
63+
ENV HOSTNAME "0.0.0.0"
7564

65+
# server.js is created by next build from the standalone output
66+
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
7667
CMD ["node", "server.js"]

0 commit comments

Comments
 (0)