From 9ee938453f9798ee7b578940796023d02c551238 Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Sun, 28 Jul 2024 16:05:08 +0800 Subject: [PATCH] feat: add Dockerfile --- Dockerfile | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ next.config.mjs | 5 ++++- 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a5cfcf0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,58 @@ +FROM node:21-alpine AS base + +FROM base AS deps +RUN apk add --no-cache libc6-compat +WORKDIR /app + +RUN apk --no-cache add ca-certificates wget +RUN wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub +RUN wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.28-r0/glibc-2.28-r0.apk +RUN apk add --no-cache --force-overwrite glibc-2.28-r0.apk + +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* bun.lockb* ./ +RUN \ + if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ + elif [ -f package-lock.json ]; then npm ci; \ + elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \ + elif [ -f bun.lockb ]; then npm install -g bun && bun install; \ + else echo "Lockfile not found." && exit 1; \ + fi + + +FROM base AS builder +WORKDIR /app +COPY --from=deps /app/node_modules ./node_modules +COPY . . + +ARG PROD_ENV="" +# Appends to .env.production +RUN printf "$PROD_ENV" >> .env.production + +RUN yarn build + + +FROM base AS runner +WORKDIR /app + +ENV NODE_ENV production +ENV NEXT_TELEMETRY_DISABLED 1 + +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 nextjs + +COPY --from=builder /app/public ./public +COPY --from=builder /app/.env.production ./.env.production + +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static + + + +USER nextjs + +EXPOSE 3000 + +ENV PORT 3000 +ENV HOSTNAME "0.0.0.0" + +CMD ["node", "server.js"] diff --git a/next.config.mjs b/next.config.mjs index 9b4c182..7e5fcf7 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -18,6 +18,9 @@ const withPWA = withPWAInit({ }); /** @type {import('next').NextConfig} */ -const nextConfig = {}; +const nextConfig = { + output: "standalone", + reactStrictMode: true, +}; export default bundleAnalyzer(withPWA(nextConfig));