From b3731e26331bc634122093a25774b16816107ba2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20Magalh=C3=A3es?= Date: Sun, 11 Oct 2020 14:24:41 +0200 Subject: [PATCH 1/5] docs(deployment): Add Docker Image section Add a Docker Image section with an example. --- docs/deployment.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/docs/deployment.md b/docs/deployment.md index fc1a806e9ccf86..a7fa1c31eb6bc3 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -70,6 +70,43 @@ Make sure your `package.json` has the `"build"` and `"start"` scripts: `next build` builds the production application in the `.next` folder. After building, `next start` starts a Node.js server that supports [hybrid pages](/docs/basic-features/pages.md), serving both statically generated and server-side rendered pages. +### Docker Image + +Next.js can be deployed to any hosting provider that supports Docker containers. This is the approach you should use when you're deploying to container orchestrators such as Kubernetes or Hashicorp Nomad. + +Here is a multi-stage `Dockerfile` using `node:alpine` that you can use: + +```Dockerfile +FROM node:alpine AS builder +RUN apk add --no-cache libc6-compat +WORKDIR /app +COPY package.json yarn.lock ./ +RUN yarn +COPY . . +RUN yarn build + +FROM node:alpine +WORKDIR /app + +ENV NODE_ENV production +RUN addgroup -g 1001 -S nodejs +RUN adduser -S nextjs -u 1001 +USER nextjs +EXPOSE 3000 + +COPY --from=builder /app/node_modules ./node_modules +COPY --from=builder /app/.next ./.next + +# Uncomment this in case you want to disable telemetry. +# RUN npx next telemetry disable + +CMD ["node_modules/.bin/next", "start"] +``` + +Make sure to place this in the root folder of your project. + +You can build this with `docker build . -t my-next-js-app` and run with `docker run -p 3000:3000 my-next-js-app`. + ### Static HTML Export If you’d like to do a static HTML export of your Next.js app, follow the directions on [our documentation](/docs/advanced-features/static-html-export.md). From 398037cadb9236065dc7e73dc3671adc55e23c15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20Magalh=C3=A3es?= Date: Sun, 11 Oct 2020 14:31:28 +0200 Subject: [PATCH 2/5] docs(deployment): add another example use case --- docs/deployment.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/deployment.md b/docs/deployment.md index a7fa1c31eb6bc3..03421354ee0b6b 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -72,7 +72,7 @@ Make sure your `package.json` has the `"build"` and `"start"` scripts: ### Docker Image -Next.js can be deployed to any hosting provider that supports Docker containers. This is the approach you should use when you're deploying to container orchestrators such as Kubernetes or Hashicorp Nomad. +Next.js can be deployed to any hosting provider that supports Docker containers. This is the approach you should use when you're deploying to container orchestrators such as Kubernetes or Hashicorp Nomad, or just want to run a docker container in a single node in any cloud provider. Here is a multi-stage `Dockerfile` using `node:alpine` that you can use: From f0dd1f7699e1242def102a456550cba107df0be7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20Magalh=C3=A3es?= Date: Wed, 24 Feb 2021 00:29:41 +0100 Subject: [PATCH 3/5] docs(dockerfile): separate builder, add public folder, describe why libc6-compat might be needed --- docs/deployment.md | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/docs/deployment.md b/docs/deployment.md index a9bf866a45bcb5..bb2bb9fcc6506d 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -85,15 +85,22 @@ Next.js can be deployed to any hosting provider that supports Docker containers. Here is a multi-stage `Dockerfile` using `node:alpine` that you can use: ```Dockerfile -FROM node:alpine AS builder +# Install dependencies only when needed +FROM node:alpine AS deps +# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. RUN apk add --no-cache libc6-compat WORKDIR /app COPY package.json yarn.lock ./ -RUN yarn +RUN yarn install --frozen-lockfile + +# Rebuild the source code only when needed +FROM node:alpine AS builder COPY . . +COPY --from=deps /app/node_modules ./node_modules RUN yarn build -FROM node:alpine +# Production image, copy all the files and run next +FROM node:alpine AS runner WORKDIR /app ENV NODE_ENV production @@ -102,10 +109,12 @@ RUN adduser -S nextjs -u 1001 USER nextjs EXPOSE 3000 -COPY --from=builder /app/node_modules ./node_modules +COPY --from=builder /app/next.config.js ./ +COPY --from=builder /app/public ./public COPY --from=builder /app/.next ./.next +COPY --from=builder /app/node_modules ./node_modules -# Uncomment this in case you want to disable telemetry. +# Uncomment the following line in case you want to disable telemetry. # RUN npx next telemetry disable CMD ["node_modules/.bin/next", "start"] From 5a7d48ff21e38c2952e42dd65d79aa3b09a015c0 Mon Sep 17 00:00:00 2001 From: Lee Robinson Date: Tue, 23 Feb 2021 18:36:01 -0600 Subject: [PATCH 4/5] Slight docs tweaks --- docs/deployment.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/deployment.md b/docs/deployment.md index bd46d25810f0bc..d9f5435759462d 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -80,7 +80,7 @@ Make sure your `package.json` has the `"build"` and `"start"` scripts: ### Docker Image -Next.js can be deployed to any hosting provider that supports Docker containers. This is the approach you should use when you're deploying to container orchestrators such as Kubernetes or Hashicorp Nomad, or just want to run a docker container in a single node in any cloud provider. +Next.js can be deployed to any hosting provider that supports [Docker](https://www.docker.com/) containers. You can use this approach when deploying to container orchestrators such as [Kubernetes](https://kubernetes.io/) or [HashiCorp Nomad](https://www.nomadproject.io/), or when running inside a single node in any cloud provider. Here is a multi-stage `Dockerfile` using `node:alpine` that you can use: @@ -120,9 +120,9 @@ COPY --from=builder /app/node_modules ./node_modules CMD ["node_modules/.bin/next", "start"] ``` -Make sure to place this in the root folder of your project. +Make sure to place this Dockerfile in the root folder of your project. -You can build this with `docker build . -t my-next-js-app` and run with `docker run -p 3000:3000 my-next-js-app`. +You can build your container with `docker build . -t my-next-js-app` and run it with `docker run -p 3000:3000 my-next-js-app`. ### Static HTML Export From 618c8842bc9491803e5e0c9f0274fe3114c343ce Mon Sep 17 00:00:00 2001 From: Lee Robinson Date: Tue, 23 Feb 2021 18:37:12 -0600 Subject: [PATCH 5/5] Update docs/deployment.md --- docs/deployment.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/deployment.md b/docs/deployment.md index d9f5435759462d..546e3176cfae38 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -114,6 +114,8 @@ COPY --from=builder /app/public ./public COPY --from=builder /app/.next ./.next COPY --from=builder /app/node_modules ./node_modules +# 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. # RUN npx next telemetry disable