From 3bb6ffab66c9c204fff1d73b62ba2505c37b78bb Mon Sep 17 00:00:00 2001 From: JakobKlotz Date: Tue, 26 Aug 2025 09:33:45 +0200 Subject: [PATCH 1/9] docs (examples): add `Dockerfile` for `bun` --- examples/with-docker/Dockerfile.bun | 60 +++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 examples/with-docker/Dockerfile.bun diff --git a/examples/with-docker/Dockerfile.bun b/examples/with-docker/Dockerfile.bun new file mode 100644 index 0000000000000..a57f47b41f100 --- /dev/null +++ b/examples/with-docker/Dockerfile.bun @@ -0,0 +1,60 @@ +# ----------------------------------------------------------------------------- +# This Dockerfile.bun is specifically configured for projects using Bun +# For npm/pnpm or yarn, refer to the Dockerfile instead +# ----------------------------------------------------------------------------- + +# Use Bun's official image +FROM oven/bun:1 AS base + +WORKDIR /app + +# Install dependencies with bun +FROM base AS deps +COPY package.json bun.lock* ./ +RUN bun install --no-save --frozen-lockfile + +# Rebuild the source code only when needed +FROM base AS builder +WORKDIR /app +COPY --from=deps /app/node_modules ./node_modules +COPY . . + +# Next.js collects completely anonymous telemetry data about general usage. +# Learn more here: https://nextjs.org/telemetry +# Uncomment the following line in case you want to disable telemetry during the build. +# ENV NEXT_TELEMETRY_DISABLED=1 + +# Create next.config.js for standalone output, if it doesn't exist +RUN if [ ! -f next.config.js ] && [ ! -f next.config.mjs ]; then \ + echo "/** @type {import('next').NextConfig} */" > next.config.js && \ + echo "const nextConfig = { output: 'standalone' };" >> next.config.js && \ + echo "module.exports = nextConfig;" >> next.config.js; \ + fi + +RUN bun run build + +# Production image, copy all the files and run next +FROM base AS runner +WORKDIR /app + +ENV NODE_ENV=production \ + # Uncomment the following line in case you want to disable telemetry during runtime. + # ENV NEXT_TELEMETRY_DISABLED=1 \ + PORT=3000 \ + HOSTNAME="0.0.0.0" + +RUN addgroup --system --gid 1001 nodejs && \ + adduser --system --uid 1001 nextjs + +COPY --from=builder /app/public ./public + +# 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 + +CMD ["node", "server.js"] From cf4d51894ecf55fed49924be260365774a244b4e Mon Sep 17 00:00:00 2001 From: JakobKlotz Date: Tue, 26 Aug 2025 09:40:44 +0200 Subject: [PATCH 2/9] docs (examples): add instructions for `bun` --- examples/with-docker/README.md | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/examples/with-docker/README.md b/examples/with-docker/README.md index d61fc6b2dc190..a5f38e7f9c4a0 100644 --- a/examples/with-docker/README.md +++ b/examples/with-docker/README.md @@ -4,7 +4,7 @@ This examples shows how to use Docker with Next.js based on the [deployment docu ## How to use -Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example: +Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), [pnpm](https://pnpm.io) or [bun](https://bun.com/) to bootstrap the example: ```bash npx create-next-app --example with-docker nextjs-docker @@ -18,17 +18,28 @@ yarn create next-app --example with-docker nextjs-docker pnpm create next-app --example with-docker nextjs-docker ``` +```bash +bun create next-app --example with-docker nextjs-docker +``` + ## Using Docker 1. [Install Docker](https://docs.docker.com/get-docker/) on your machine. -1. Build your container: `docker build -t nextjs-docker .`. -1. Run your container: `docker run -p 3000:3000 nextjs-docker`. +2. Build your container: + ```bash + # For npm, pnpm or yarn + docker build -t nextjs-docker . + + # For bun + docker build -f Dockerfile.bun -t nextjs-docker . + ``` +3. Run your container: `docker run -p 3000:3000 nextjs-docker`. You can view your images created with `docker images`. ### In existing projects -To add support for Docker to an existing project, just copy the [`Dockerfile`](https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile) into the root of the project and add the following to the `next.config.js` file: +To add support for Docker to an existing project, just copy the [`Dockerfile`](https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile) into the root of the project (or [`Dockerfile.bun`](https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile.bun) if you're using bun) and add the following to the `next.config.js` file: ```js // next.config.js @@ -43,10 +54,10 @@ This will build the project as a standalone app inside the Docker image. ## Deploying to Google Cloud Run 1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/docs/install) so you can use `gcloud` on the command line. -1. Run `gcloud auth login` to log in to your account. -1. [Create a new project](https://cloud.google.com/run/docs/quickstarts/build-and-deploy) in Google Cloud Run (e.g. `nextjs-docker`). Ensure billing is turned on. -1. Build your container image using Cloud Build: `gcloud builds submit --tag gcr.io/PROJECT-ID/helloworld --project PROJECT-ID`. This will also enable Cloud Build for your project. -1. Deploy to Cloud Run: `gcloud run deploy --image gcr.io/PROJECT-ID/helloworld --project PROJECT-ID --platform managed --allow-unauthenticated`. Choose a region of your choice. +2. Run `gcloud auth login` to log in to your account. +3. [Create a new project](https://cloud.google.com/run/docs/quickstarts/build-and-deploy) in Google Cloud Run (e.g. `nextjs-docker`). Ensure billing is turned on. +4. Build your container image using Cloud Build: `gcloud builds submit --tag gcr.io/PROJECT-ID/helloworld --project PROJECT-ID`. This will also enable Cloud Build for your project. +5. Deploy to Cloud Run: `gcloud run deploy --image gcr.io/PROJECT-ID/helloworld --project PROJECT-ID --platform managed --allow-unauthenticated`. Choose a region of your choice. - You will be prompted for the service name: press Enter to accept the default name, `helloworld`. - You will be prompted for [region](https://cloud.google.com/run/docs/quickstarts/build-and-deploy#follow-cloud-run): select the region of your choice, for example `us-central1`. @@ -59,6 +70,8 @@ First, run the development server: npm run dev # or yarn dev +# or +bun run dev ``` Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. From 094484fcfc58cfe252fe463000a84a4523dbce73 Mon Sep 17 00:00:00 2001 From: JakobKlotz Date: Tue, 26 Aug 2025 15:20:27 +0200 Subject: [PATCH 3/9] docs (examples): fix multi-line ENV statement --- examples/with-docker/Dockerfile.bun | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/with-docker/Dockerfile.bun b/examples/with-docker/Dockerfile.bun index a57f47b41f100..4912ae3854ad2 100644 --- a/examples/with-docker/Dockerfile.bun +++ b/examples/with-docker/Dockerfile.bun @@ -37,9 +37,10 @@ RUN bun run build FROM base AS runner WORKDIR /app +# Uncomment the following line in case you want to disable telemetry during runtime. +# ENV NEXT_TELEMETRY_DISABLED=1 + ENV NODE_ENV=production \ - # Uncomment the following line in case you want to disable telemetry during runtime. - # ENV NEXT_TELEMETRY_DISABLED=1 \ PORT=3000 \ HOSTNAME="0.0.0.0" From 7e11fb1232adf6f81278e7bc4bda29ecaeef3549 Mon Sep 17 00:00:00 2001 From: Jakob Klotz Date: Tue, 2 Sep 2025 06:54:13 +0200 Subject: [PATCH 4/9] docs (examples): remove creation of next.config.js for more consistency Existing docker example within the same directory does not create a next.config.js for standalone output (within the Dockerfile), hence it's now more consistent. Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com> --- examples/with-docker/Dockerfile.bun | 7 ------- 1 file changed, 7 deletions(-) diff --git a/examples/with-docker/Dockerfile.bun b/examples/with-docker/Dockerfile.bun index 4912ae3854ad2..7617d65288928 100644 --- a/examples/with-docker/Dockerfile.bun +++ b/examples/with-docker/Dockerfile.bun @@ -24,13 +24,6 @@ COPY . . # Uncomment the following line in case you want to disable telemetry during the build. # ENV NEXT_TELEMETRY_DISABLED=1 -# Create next.config.js for standalone output, if it doesn't exist -RUN if [ ! -f next.config.js ] && [ ! -f next.config.mjs ]; then \ - echo "/** @type {import('next').NextConfig} */" > next.config.js && \ - echo "const nextConfig = { output: 'standalone' };" >> next.config.js && \ - echo "module.exports = nextConfig;" >> next.config.js; \ - fi - RUN bun run build # Production image, copy all the files and run next From a7936a2f317996f1ced97732bd5651acf6ae7393 Mon Sep 17 00:00:00 2001 From: JakobKlotz Date: Wed, 3 Sep 2025 08:03:32 +0200 Subject: [PATCH 5/9] fix (examples): replace `node` with `bun` as runtime --- examples/with-docker/Dockerfile.bun | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/with-docker/Dockerfile.bun b/examples/with-docker/Dockerfile.bun index 7617d65288928..bd4ff02f8964b 100644 --- a/examples/with-docker/Dockerfile.bun +++ b/examples/with-docker/Dockerfile.bun @@ -51,4 +51,4 @@ USER nextjs EXPOSE 3000 -CMD ["node", "server.js"] +CMD ["bun", "run", "server.js"] From a8f1ab0d39bb90afe9a082467c6f4bc13478533d Mon Sep 17 00:00:00 2001 From: JakobKlotz Date: Wed, 3 Sep 2025 10:41:12 +0200 Subject: [PATCH 6/9] docs (examples): revert list numbering change --- examples/with-docker/README.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/examples/with-docker/README.md b/examples/with-docker/README.md index a5f38e7f9c4a0..62bcf630e7c59 100644 --- a/examples/with-docker/README.md +++ b/examples/with-docker/README.md @@ -25,15 +25,15 @@ bun create next-app --example with-docker nextjs-docker ## Using Docker 1. [Install Docker](https://docs.docker.com/get-docker/) on your machine. -2. Build your container: - ```bash - # For npm, pnpm or yarn - docker build -t nextjs-docker . - - # For bun - docker build -f Dockerfile.bun -t nextjs-docker . - ``` -3. Run your container: `docker run -p 3000:3000 nextjs-docker`. +1. Build your container: + ```bash + # For npm, pnpm or yarn + docker build -t nextjs-docker . + + # For bun + docker build -f Dockerfile.bun -t nextjs-docker . + ``` +1. Run your container: `docker run -p 3000:3000 nextjs-docker`. You can view your images created with `docker images`. @@ -54,10 +54,10 @@ This will build the project as a standalone app inside the Docker image. ## Deploying to Google Cloud Run 1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/docs/install) so you can use `gcloud` on the command line. -2. Run `gcloud auth login` to log in to your account. -3. [Create a new project](https://cloud.google.com/run/docs/quickstarts/build-and-deploy) in Google Cloud Run (e.g. `nextjs-docker`). Ensure billing is turned on. -4. Build your container image using Cloud Build: `gcloud builds submit --tag gcr.io/PROJECT-ID/helloworld --project PROJECT-ID`. This will also enable Cloud Build for your project. -5. Deploy to Cloud Run: `gcloud run deploy --image gcr.io/PROJECT-ID/helloworld --project PROJECT-ID --platform managed --allow-unauthenticated`. Choose a region of your choice. +1. Run `gcloud auth login` to log in to your account. +1. [Create a new project](https://cloud.google.com/run/docs/quickstarts/build-and-deploy) in Google Cloud Run (e.g. `nextjs-docker`). Ensure billing is turned on. +1. Build your container image using Cloud Build: `gcloud builds submit --tag gcr.io/PROJECT-ID/helloworld --project PROJECT-ID`. This will also enable Cloud Build for your project. +1. Deploy to Cloud Run: `gcloud run deploy --image gcr.io/PROJECT-ID/helloworld --project PROJECT-ID --platform managed --allow-unauthenticated`. Choose a region of your choice. - You will be prompted for the service name: press Enter to accept the default name, `helloworld`. - You will be prompted for [region](https://cloud.google.com/run/docs/quickstarts/build-and-deploy#follow-cloud-run): select the region of your choice, for example `us-central1`. From 77c91279d189bea5ac1ae203fb24e06767828f2e Mon Sep 17 00:00:00 2001 From: Jakob Klotz Date: Wed, 3 Sep 2025 12:37:54 +0200 Subject: [PATCH 7/9] Update examples/with-docker/README.md Co-authored-by: Joseph --- examples/with-docker/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/with-docker/README.md b/examples/with-docker/README.md index 62bcf630e7c59..f783ccbc311e8 100644 --- a/examples/with-docker/README.md +++ b/examples/with-docker/README.md @@ -39,7 +39,7 @@ You can view your images created with `docker images`. ### In existing projects -To add support for Docker to an existing project, just copy the [`Dockerfile`](https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile) into the root of the project (or [`Dockerfile.bun`](https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile.bun) if you're using bun) and add the following to the `next.config.js` file: +To add Docker support, copy [`Dockerfile`](https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile) to the project root. If using Bun, copy [`Dockerfile.bun`](https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile.bun) instead. Then add the following to next.config.js: ```js // next.config.js From 6cae3838667b6fd589395d8e697ae3032c66eb4b Mon Sep 17 00:00:00 2001 From: Jakob Klotz Date: Wed, 3 Sep 2025 14:53:43 +0200 Subject: [PATCH 8/9] Update examples/with-docker/Dockerfile.bun --- examples/with-docker/Dockerfile.bun | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/with-docker/Dockerfile.bun b/examples/with-docker/Dockerfile.bun index bd4ff02f8964b..a7cdafaf9c7e4 100644 --- a/examples/with-docker/Dockerfile.bun +++ b/examples/with-docker/Dockerfile.bun @@ -51,4 +51,4 @@ USER nextjs EXPOSE 3000 -CMD ["bun", "run", "server.js"] +CMD ["bun", "./server.js"] From dd6c3b2d23e9a424e3ed26b3c03f0de19c471e4e Mon Sep 17 00:00:00 2001 From: Joseph Date: Wed, 3 Sep 2025 23:54:24 +0200 Subject: [PATCH 9/9] Update examples/with-docker/README.md --- examples/with-docker/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/with-docker/README.md b/examples/with-docker/README.md index f783ccbc311e8..592b41773b4dd 100644 --- a/examples/with-docker/README.md +++ b/examples/with-docker/README.md @@ -4,7 +4,7 @@ This examples shows how to use Docker with Next.js based on the [deployment docu ## How to use -Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), [pnpm](https://pnpm.io) or [bun](https://bun.com/) to bootstrap the example: +Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), [pnpm](https://pnpm.io) or [bun](https://bun.sh/docs/cli/bun-create) to bootstrap the example: ```bash npx create-next-app --example with-docker nextjs-docker