Skip to content

Commit ef0480d

Browse files
committed
simplify dockerfile, add dockerignore, use standalone output in production, reduce image size significantly
1 parent 4823cd2 commit ef0480d

File tree

3 files changed

+30
-51
lines changed

3 files changed

+30
-51
lines changed

src/templates/general/.dockerignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Dockerfile
2+
.dockerignore
3+
node_modules
4+
npm-debug.log
5+
README.md
6+
.next
7+
.git

src/templates/general/Dockerfile

+22-51
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,37 @@
11
# Based on https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile
2-
32
FROM node:20.5.0-alpine AS base
43

54
# Install dependencies only when needed
6-
FROM base AS builder
5+
FROM base AS deps
76
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
87
RUN apk add --no-cache libc6-compat
98
WORKDIR /app
109

1110
<§yarn§># Yarn --------------------------------------------------------------------------
12-
# NEW enable yarn 4.0.2 version and copy yarnrc.yml
1311
RUN corepack enable
1412
COPY .yarn ./.yarn
1513
</§yarn§>
16-
# Install dependencies based on the preferred package manager (NEW copy yarnrc.yml to the image)
17-
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* .yarnrc.yml* ./
18-
19-
# use this line if you have dependencies that for some reason need global typescript
20-
# RUN npm install typescript -g
2114

15+
# Install dependencies based on the preferred package manager
16+
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* .npmrc* .yarnrc.yml* ./
2217
RUN \
2318
if [ -f yarn.lock ]; then yarn --immutable; \
24-
elif [ -f package-lock.json ]; then npm ci; \
19+
elif [ -f package-lock.json ]; then npm ci --force; \
20+
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
2521
else echo "Lockfile not found." && exit 1; \
2622
fi
2723

28-
2924
# Rebuild the source code only when needed
25+
FROM base AS builder
3026
WORKDIR /app
31-
# COPY --from=builder /app/.yarn ./.yarn
32-
# COPY --from=builder /app/node_modules ./node_modules
27+
28+
COPY --from=deps /app/node_modules ./node_modules
3329
COPY . .
3430

3531
# Next.js collects completely anonymous telemetry data about general usage.
3632
# Learn more here: https://nextjs.org/telemetry
37-
# Uncomment the following line in case you want to disable telemetry during the build.
38-
ENV NEXT_TELEMETRY_DISABLED 1
33+
ENV NEXT_TELEMETRY_DISABLED=1
34+
ENV BUILD_STANDALONE=1
3935

4036
RUN \
4137
if [ -f yarn.lock ]; then <§prisma§>yarn prisma generate && </§prisma§>yarn run build; \
@@ -47,57 +43,32 @@ fi
4743
FROM base AS runner
4844
WORKDIR /app
4945

50-
ENV NODE_ENV production
46+
ENV NODE_ENV=production
5147
# Uncomment the following line in case you want to disable telemetry during runtime.
52-
ENV NEXT_TELEMETRY_DISABLED 1
48+
ENV NEXT_TELEMETRY_DISABLED=1
5349

5450
RUN addgroup --system --gid 1001 nodejs
5551
RUN adduser --system --uid 1001 nextjs
5652

57-
# adjust files to your package manager and packages
58-
COPY --from=builder /app/next.config.ts ./
59-
COPY --from=builder /app/i18nConfig.js ./
60-
COPY --from=builder /app/public ./public
61-
COPY --from=builder /app/.next ./.next
62-
COPY --from=builder /app/package.json ./package.json
63-
<§yarn§># Yarn --------------------------------------------------------------------------
64-
COPY --from=builder /app/.yarn ./.yarn
65-
COPY --from=builder /app/yarn.lock ./yarn.lock
66-
COPY --from=builder /app/.yarnrc.yml ./.yarnrc.yml
67-
</§yarn§><§npm§># NPM ---------------------------------------------------------------------------
68-
COPY --from=builder /app/node_modules ./node_modules
69-
COPY --from=builder /app/package-lock.json ./package-lock.json
70-
</§npm§><§prisma§># Prisma ------------------------------------------------------------------------
71-
COPY --from=builder /app/prisma ./prisma
72-
</§prisma§>
73-
# use this line if you have dependencies that for some reason need global typescript
74-
# RUN npm install typescript -g
75-
76-
# Note rebuild again - this is to let the package manager rebuild binaries in the "runner" stage of the Dockerfile
77-
<§yarn§># We also have to remove unplugged, so that rebuilding happens and replaces the old binaries
78-
RUN rm -rf /app/.yarn/unplugged && yarn rebuild
79-
</§yarn§>
80-
8153
# Set the correct permission for prerender cache
82-
RUN chown -R nextjs:nodejs /app/.next
83-
<§yarn§>RUN chown -R nextjs:nodejs /app/.yarn
84-
</§yarn§>
54+
RUN mkdir .next
55+
RUN chown -R nextjs:nodejs .next
8556

57+
<§prisma§># Prisma ------------------------------------------------------------------------
58+
COPY --from=builder /app/prisma ./prisma
59+
</§prisma§>
8660
# Automatically leverage output traces to reduce image size
8761
# https://nextjs.org/docs/advanced-features/output-file-tracing
88-
# COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
89-
# COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
62+
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
63+
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
9064

9165
USER nextjs
9266

9367
EXPOSE 3000
9468

95-
ENV PORT 3000
96-
# set hostname to localhost
97-
# ENV HOSTNAME "0.0.0.0"
69+
ENV PORT=3000
9870

9971
# server.js is created by next build from the standalone output
10072
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
101-
<§yarn§>CMD ["yarn", "start"]
102-
</§yarn§><§npm§>CMD ["npm", "run", "start"]
103-
</§npm§>
73+
ENV HOSTNAME="0.0.0.0"
74+
CMD ["node", "server.js"]

src/templates/general/next.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { NextConfig } from 'next'
22

33
const nextConfig: NextConfig = {
44
/* config options here */
5+
output: process.env.BUILD_STANDALONE === '1' ? 'standalone' : undefined,
56
poweredByHeader: false,
67
reactStrictMode: true,
78
}

0 commit comments

Comments
 (0)