Browse Source

perf: build in stages to reduce container size

AprilNEA 1 year ago
parent
commit
2645540721
2 changed files with 38 additions and 4 deletions
  1. 34 4
      Dockerfile
  2. 4 0
      next.config.js

+ 34 - 4
Dockerfile

@@ -1,14 +1,44 @@
-FROM node:18
+FROM node:18-alpine AS base
+
+FROM base AS deps
+
+RUN apk add --no-cache libc6-compat
 
 WORKDIR /app
 
+COPY package.json yarn.lock* package-lock.json* ./
+
+RUN \
+  if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
+  elif [ -f package-lock.json ]; then npm ci; \
+  else echo "Lockfile not found." && exit 1; \
+  fi
+
+FROM base AS builder
+
+RUN apk update && apk add --no-cache git
+
 ENV OPENAI_API_KEY=""
 ENV CODE=""
+ARG DOCKER=true
 
-COPY . ./
+WORKDIR /app
+COPY --from=deps /app/node_modules ./node_modules
+COPY . .
+
+RUN yarn build
+
+FROM base AS runner
+WORKDIR /app
+
+ENV OPENAI_API_KEY=""
+ENV CODE=""
 
-RUN yarn && yarn build
+COPY --from=builder /app/public ./public
+COPY --from=builder /app/.next/standalone ./
+COPY --from=builder /app/.next/static ./.next/static
+COPY --from=builder /app/.next/server ./.next/server
 
 EXPOSE 3000
 
-CMD ["yarn","start"]
+CMD ["node","server.js"]

+ 4 - 0
next.config.js

@@ -14,4 +14,8 @@ const nextConfig = {
   }
 };
 
+if (process.env.DOCKER) {
+  nextConfig.output = 'standalone'
+}
+
 module.exports = nextConfig;