perf: build in stages to reduce container size

This commit is contained in:
AprilNEA 2023-03-27 13:49:26 +08:00
parent 8d0d08725d
commit 2645540721
No known key found for this signature in database
GPG Key ID: B93E17BB436B4DE1
2 changed files with 39 additions and 5 deletions

View File

@ -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
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN yarn build
FROM base AS runner
WORKDIR /app WORKDIR /app
ENV OPENAI_API_KEY="" ENV OPENAI_API_KEY=""
ENV CODE="" ENV CODE=""
COPY . ./ COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
RUN yarn && yarn build COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/.next/server ./.next/server
EXPOSE 3000 EXPOSE 3000
CMD ["yarn","start"] CMD ["node","server.js"]

View File

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