From 04b75cc18e569bac0d93e034be2283097698aecb Mon Sep 17 00:00:00 2001 From: Emmanuel Gautier Date: Thu, 8 Aug 2024 18:34:33 +0200 Subject: [PATCH] build: add nextjs dockerfile --- examples/nextjs-app/Dockerfile | 43 +++++++++++++++++++++++++++++ examples/nextjs-app/next.config.mjs | 4 ++- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 examples/nextjs-app/Dockerfile diff --git a/examples/nextjs-app/Dockerfile b/examples/nextjs-app/Dockerfile new file mode 100644 index 0000000..897faed --- /dev/null +++ b/examples/nextjs-app/Dockerfile @@ -0,0 +1,43 @@ +FROM node:20 as builder + +WORKDIR /app + +COPY package*.json ./ +RUN npm ci + +COPY . . + +ENV NEXT_BUILD_OUTPUT 'standalone' + +RUN npm run build + +FROM node:20-alpine + +WORKDIR /app + +ENV NODE_ENV production + +RUN addgroup --system --gid 1001 nodejs && \ + adduser --system --uid 1001 nextjs + +COPY --from=builder /app/public ./public + +# Set the correct permission for prerender cache +RUN mkdir .next && \ + chown nextjs:nodejs .next + +# Automatically leverage output traces to reduce image size +# https://nextjs.org/docs/advanced-features/output-file-tracing +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" + +# server.js is created by next build from the standalone output +# https://nextjs.org/docs/pages/api-reference/next-config-js/output +CMD ["node", "server.js"] diff --git a/examples/nextjs-app/next.config.mjs b/examples/nextjs-app/next.config.mjs index 4678774..f26f484 100644 --- a/examples/nextjs-app/next.config.mjs +++ b/examples/nextjs-app/next.config.mjs @@ -1,4 +1,6 @@ /** @type {import('next').NextConfig} */ -const nextConfig = {}; +const nextConfig = { + output: process.env.NEXT_BUILD_OUTPUT, +}; export default nextConfig;